Quantcast
Channel: Questions in topic: "error message"
Viewing all articles
Browse latest Browse all 2891

Keep getting Null reference error for AI Script

$
0
0
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(); character = GetComponent(); 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?

Viewing all articles
Browse latest Browse all 2891

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>