• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by HAWK101 · Jun 14, 2017 at 06:57 PM · scripting problemerror messageai problems

Keep getting Null reference error for AI Script

So I''m using this AI script that has the AI going to waypoints by default. When a player goes into the trigger placed around the AI it changes the state of the AI to CHASE instead of PATROL. But the default script is not working at all and this is the error:

NullReferenceException: Object reference not set to an instance of an object UnityStandardAssets.Characters.ThirdPerson.ThirdPersonCharacter.CheckGroundStatus () (at Assets/Standard Assets/Characters/ThirdPersonCharacter/Scripts/ThirdPersonCharacter.cs:221) UnityStandardAssets.Characters.ThirdPerson.ThirdPersonCharacter.Move (Vector3 move, Boolean crouch, Boolean jump) (at Assets/Standard Assets/Characters/ThirdPersonCharacter/Scripts/ThirdPersonCharacter.cs:54) UnityStandardAssets.Characters.ThirdPerson.basicAI.Patrol () (at Assets/Scripts/AI/basicAI.cs:71) UnityStandardAssets.Characters.ThirdPerson.basicAI+c__Iterator0.MoveNext () (at Assets/Scripts/AI/basicAI.cs:54) UnityEngine.SetupCoroutine.InvokeMoveNext (IEnumerator enumerator, IntPtr returnValueAddress) (at C:/buildslave/unity/build/Runtime/Export/Coroutines.cs:17) UnityEngine.MonoBehaviour:StartCoroutine(String) UnityStandardAssets.Characters.ThirdPerson.basicAI:Start() (at Assets/Scripts/AI/basicAI.cs:44)

This is the script used:

using System.Collections; using System.Collections.Generic; using UnityEngine; using System; using UnityEngine.AI;

namespace UnityStandardAssets.Characters.ThirdPerson { public class basicAI : MonoBehaviour {

     public NavMeshAgent agent;
     public ThirdPersonCharacter character;

     public enum State {
         PATROL,
         CHASE
     }

     public State state;
     private bool alive;

     // Variables For Patrolling
     public GameObject[] waypoints;
     private int waypointInd = 0;
     public float patrolSpeed = 0.5f;

     // Variables For Chasing
     public float chaseSpeed = 1f;
     public GameObject target;

     // Use this for initialization
     void Start () {
         agent = GetComponent<NavMeshAgent>();
         character = GetComponent<ThirdPersonCharacter>();

         agent.updatePosition = true;
         agent.updateRotation = false;

         state = basicAI.State.PATROL;

         alive = true;

         StartCoroutine("FSM");
     }

     IEnumerator FSM()
     {
         while (alive)
         {
             switch (state)
             {
                 case State.PATROL:
                     Patrol ();
                     break;
                 case State.CHASE:
                     Chase ();
                     break;
             }
             yield return null;
         }

     }

     void Patrol()
     {
         agent.speed = patrolSpeed;
         if (Vector3.Distance (this.transform.position, waypoints[waypointInd].transform.position) >= 2)
         {
             agent.SetDestination(waypoints[waypointInd].transform.position);
             character.Move (agent.desiredVelocity, false, false);
         }
         else if (Vector3.Distance (this.transform.position, waypoints[waypointInd].transform.position) <= 2)
         {
             waypointInd += 1;
             if (waypointInd > waypoints.Length)
             {
                 waypointInd = 0;
             }
         }
         else
         {
             character.Move (Vector3.zero, false, false);
         }

     }

     void Chase()
     {
         agent.speed = chaseSpeed;
         agent.SetDestination(target.transform.position);
         character.Move(agent.desiredVelocity, false, false);
     }

     void OnTriggerEnter (Collider coll)
     {
         if (coll.tag == "Player")
         {
             state = basicAI.State.CHASE;
             target = coll.gameObject;
         }

     }
 }

}

Does anyone know why this keeps happening and have a solution to fix this?

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image lyc0731 · Apr 24 at 08:00 AM 0
Share

same problem, but I did not use waypoints. But basically I followed that tutorial. How did you solve that.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Zezzman · Jul 10, 2017 at 02:07 PM

Try changing the variable "waypoints" type from GameObject [] to List. Use List types instead of Arrays because its better for dynamic arrays and use a null check statement when using a dynamic objects arrays by:

 GameObject GO = waypoints[waypointInd];
 if(GO != null){
 //Do something
 }

and i would say to rather use "Vector3" then "GameObject".

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

144 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Please Help Me: Assets/Scripts/PlayerControaller.cs(34,33): error CS8025: Parsing error 1 Answer

cannot convert type HealthandInventory to int 0 Answers

How do I fix "universul?" script problem 0 Answers

Object reference not set to an instance of an object - Jumping Scripts (C#) 1 Answer

I Need help with scripting a first person camera? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges