Problem with Destroy () and instantiated prefabs. (MissingReferenceException)

Hi, I have a problem doing a Destroy () with instantiated Prefabs. This is the code that instantiates the prefabs. (I removed all unnecessary parts to understand my problem).

 {
 // Spawn_Script
     public GameObject prefabModel;
     public GameObject instantiatePrefab;
     private Vector3 position = new Vector3 (0, 2. 0);
     
     public void Spawn()
     {
      for(int i=0; i<8; i++){
     instantiatePrefab = Instantiate (prefabModel, position , transform.rotation);
 }
     }
  }

Each instantiated prefab has the same script which simply checks if it comes into contact with a collider. here is a small example of the code (at the top I refer to a GameObject that includes a script that calls events inside this code that we will call Npc_Controller.

     {
     // Npc_Controller
     
     public NavMeshAgent myNavmeshAgent;
         EventController eventController;

private void Start()
{
EventController = GameObject.FindObjectOfType<Event_Controller>();
}
     
     private void OnTriggerExit(Collider other)
         {
             switch (other.tag)
             {
                 case "DestroyPoint": 
                     Destroy(gameObject);
                     break;
             }
     }

The third code only deals with calling events, if you need to attach part of that code as well.

As soon as my code gets to the point of destroying one of the instantiated NPCs, unity throws this error and none of the npc control script related events work anymore. (By destroying only one instanced NPC, none of the other NPCs in the game receive the events anymore) I am also attaching the error code that unity generates.


Error generated:

MissingReferenceException: The object
of type ‘Npc_Controller’ has been
destroyed but you are still trying to
access it. Your script should either
check if it is null or you should not
destroy the object.
Npc_Controller.ExampleEvemt () (at
Assets/Scripts/Npc_Controller.cs:102)
Event_Controller.CallEvent () (at
Assets/Scripts/Event_Controller.cs:177)


I want to clarify that this is the simplified code, just so as not to create confusion about what the problem could be.

I apologize if my problem may be nonsense to many, but after several hours spent solving this problem I could not get over it.

Thanks :slight_smile:

Error is pretty straightforward: MissingReferenceException: The object of type ‘Npc_Controller’ has been destroyed but you are still trying to access it. Your script should either check if it is null or you should not destroy the object. Npc_Controller.ExampleEvemt () (at Assets/Scripts/Npc_Controller.cs:102) Event_Controller.CallEvent () (at Assets/Scripts/Event_Controller.cs:177)

In your scripts Npc_Controller.cs line 102 and Event_Controller.cs line 177 you are accessing an object of type Npc_Controller, but it is null because you destroyed it. The error message tells you to check if it’s null before trying to access it, that is a good solution.

It looks like you did not include the part of the code where the error happens, this makes it hard to help, you don’t even show where you declare the variable causing the error, you are trying to simplify it, but you left out all the relevant code… but I can try to help, before you access your object check if it’s null

if(yourObject != null){ do stuff }