- Home /
 
OnTriggerEnter -- NullReferenceException -- C#
here is the code I Recieve NullReferenceException: Object reference not set to an instance of an object NPC_Battle_AI.OnTriggerEnter (UnityEngine.Collider col) (at Assets/Assets/Scripts/NPC_Battle_AI.cs:82)
I added a self check but it still popped up null. I have attached the script. Can anyone Help?
 using UnityEngine;
 using System.Collections;
 
 public class NPC_Battle_AI : MonoBehaviour {
 
     public GameObject NPC;
     public float rotationSpeed;
     public GameObject target;
     public bool move;
     public bool attack;
 
     public GameObject[] projectileSpawn;
     public GameObject projectile;
     private GameObject missile1 ;
     private GameObject missile2 ;
     public float reloadTine;
     public GameObject closest;
     public bool openBattle;
 
     void Start ()
     {
         if (target == null)
         {
             openBattle = true;
             move = false;
             NPC = gameObject;
             rotationSpeed = 5;
         FindClosestEnemy(target);
         }
     }
 
     void Update ()
     {
 
         if (move == true)
         {
         NPC.transform.rotation = Quaternion.Slerp (NPC.transform.rotation, Quaternion.LookRotation (target.transform.position - NPC.transform.position), rotationSpeed * Time.deltaTime);
         transform.position = Vector3.MoveTowards (transform.position,target.transform.position, GetComponent <Stats_Script>().speed *Time.deltaTime );
         }
         else if (attack == true)
         {
             Attack ();
 
             if (target == null)
             {
                 attack = false;
                 GetComponent <NPC_Battle_AI>().enabled = false;
                 gameObject.GetComponent<Basic_AI>().enabled = true;
 
             }
         }
     }    
 
     void FindClosestEnemy(GameObject target) {
         if (openBattle == true )
             {
             GameObject[] gos;
             gos = GameObject.Find ("Main Camera").GetComponent <Tower_Database>().registry;
 
             GameObject closest;
             float distance = Mathf.Infinity;
             Vector3 position = transform.position;
             foreach (GameObject go in gos) {
                 Vector3 diff = go.transform.position - position;
                 float curDistance = diff.sqrMagnitude;
                 if (curDistance < distance) {
                     closest = go;
                     target = closest;
                     distance = curDistance;
                 }
             }
     //        return closest;
         }
     }
 
     void OnTriggerEnter (Collider col)
     {
         if (col != null)
         {
         if (openBattle == true)
         {
                 if (col.gameObject.GetComponent <Stats_Script>().userActive == true)
                 {
                     target = col.gameObject;
                     move = false;
                     attack = true;
                 }
             }
         }
     }
 
     void Attack ()
     {
         attack = false;
         if (projectileSpawn.Length == 0)
         {
             gameObject.GetComponent <NPC_Battle_AI>().enabled = false;
             gameObject.GetComponent <Basic_AI>().enabled = true;
         }
 
         else {
             missile1 = Instantiate (projectile, projectileSpawn[0].transform.position, projectileSpawn[0].transform.rotation) as GameObject;
             if (projectileSpawn.Length > 1 )
             {
                 missile2 = Instantiate (projectile, projectileSpawn[1].transform.position, projectileSpawn[1].transform.rotation) as GameObject;
             }
 
             StartCoroutine (Wait());
         }
     }
     
     IEnumerator Wait ()
     {
         print (gameObject.name + " Reload.");
         yield return new WaitForSeconds (reloadTine);
         attack = true;
     }
 
 }
 
 
              Answer by Khada · Aug 25, 2014 at 05:14 AM
The '.userActive' assumes that 'GetComponent' found a component of the type 'Stats_Script' on the 'gameObject' from 'col'. It would seem to be a false assumption.
Try storing the return value of 'GetComponent' and checking if it's null before using it.
 Stats_Script s = col.gameObject.GetComponent <Stats_Script>();
 if(s != null && s.userActive == true)
     //...
 
              Answer by Tomer-Barkan · Aug 25, 2014 at 05:17 AM
Like robertu said, most likely the colliding object does not contain the script Stats_Script on it.
You should add a test for this in your code, and ignore colliders that do not have this script:
 Stats_Script statsScript = col.gameObject.GetComponent <Stats_Script>();
 if (statsScript != null && statsScript.userActive == true)
 {
     target = col.gameObject;
     move = false;
     attack = true;
 }
 
              Your answer