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?
same problem, but I did not use waypoints. But basically I followed that tutorial. How did you solve that.
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".
Your answer
Follow this Question
Related Questions
Particle System trigger script not working,Particle system trigger script not working 0 Answers
how do i get enemy ai to avoid objects in scene 1 Answer
Need help with error CS0029 and error CS0118 3 Answers
Referencing and Executing Script Can't Be Done due to Not Being A Statement 0 Answers
AI Problem. 0 Answers