Respawn Management

Good Afternoon :slight_smile:
Currently, I’m making a simple 2D game
In order to respawn enemies, I’ve made a respawn manager. It works perfectly when I run it. However, after few seconds, it just stops working
119291-2.png
119292-1.png
As you can see, there’s enemy prefab at the first pictiure. But it suddenly disappears after few second (Pic2)

And this kind of error message comes out

“MissingReferenceException: The object of type ‘GameObject’ has been destroyed but you are still trying to access it”

How should I solve this problem??
Please read the comments, too.
Thank You :slight_smile:

This is very strange, you saying the variable Obj gets null for nor reason?

Is Enemy a prefab? are you sure you assigned it by draggin from some of your assets folder?

If you drag a prefab from some assets folder, and there is no code assigning new object in variable Obj, this can only be caused because the asset has changed name, or deleted or something…


That void Destroy in the second script, where is it palced? at what object?
If you want to destroy the object containig that script, the instantiated object in the scene you need to do directly:

Destroy (gameObject);

gameObject (with minusc “g” refears to the GameObject containing the script). if you write “this” i think is refearing to class so it destroys the prefab.

I guess that in your RespawnManager script, you destroy the enemy in few seconds.
I’m not sure, but I guess you destroy the Prefab gameObject.

public GameObject obj;

Destroy(obj);

This will cause your problem to happen.
So, you can destroy the instance instead of the prefab.

public GameObject obj;
GameObject enemy1 = Instantiate(obj);
Destroy(enemy1 );

If you need every enemy to destroy in a few seconds, you can create a script to delay destroy the object which the script attached to, and then drag this script to your enemy prefab.