- Home /
Respawn Management
Good Afternoon :) 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 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 :)
These might be the scripts that have problems
Answer by tormentoarmagedoom · Jun 21, 2018 at 03:16 PM
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.
Answer by LeeroyLin · Jun 21, 2018 at 03:20 PM
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.
Good day Leeroy,
Tis can work if the same script that instantiates the object, is who destroys it. But if the object needs to destroy itself, he doesnt know that "enemy1" means.
:D
Your answer
Follow this Question
Related Questions
Respawn function after character is destroyed 3 Answers
Help with error when using instantiate to respawn an enemy. 0 Answers
Can't instantiate "scene objects" [Unity Mirror Multiplayer] 0 Answers
Adjacent Placement of Objects according to varying height 0 Answers
How to instantiate and destroy objects with 2d trigger 1 Answer