GameObject still destroyed after reloading level.

I’m on a team that’s making a short game that consists of two scenes: the start screen, and the game itself. The start screen has a Play button that when clicked, loads the scene that contains the game. When the game ends (on a win, loss, etc.) the main menu is loaded again. If you click Play a second time however, I get the following error:
“MissingReferenceException: The object of type ‘GameObject’ has been destroyed but you are still trying to access it.”

I don’t understand how this is possible. I thought that when you load a level, everything is restored to its initial state. Is that not the case? And if it is, why would it work the first time, but not the second time?

We never explicitly tell Unity to destroy the gameobject, so I’m assuming it’s getting destroyed (along with everything else) when we load the main menu scene, and then for some reason it isn’t getting recreated when we load the game scene the second time. The weird thing is though, when I pause the game and look at the scene, the gameobject in question is still there in the hierarchy. Does anyone know what could be causing this?

Thanks!

Are you sure the reference to the object is being [serialized][1] properly? [1]: http://forum.unity3d.com/threads/155352-Serialization-Best-Practices-Megapost

Just set Instantiate one object in the Start(); So each level01 it will be instantiated with the same reference.

all the modifications made during gameplay are returned when you reload a level . I had the same issue as you for something, I kept reapplying an object, playing it, and the object would have disappeared in the next to go. just make sure the GameObject prefab is in the file system and it starts off on the level properly and I think it should be okay.

@RC-1290 Neat article, but does that really apply here? I'm not making a tool or anything, and the problem isn't when I enter and exit play mode, it's when a level is reloaded using Application.LoadLevel. @Arpian I don't understand what you mean. Can you point me to an example? @ZoomDomain Are you talking about things in the asset folder? As far as I know, nothing's happening to the assets. And everything works fine when I first start, it's when the level gets reloaded. Thank you all for the answers. I'm sorry I just don't quite understand them!

2 Answers

2

I’m guessing the script throwing the error is set to “DoNotDestroyOnLoad” so it’s reference variable isn’t being reset. Since the object is back again when the scene loads but no longer attached to the script, simply have it’s Update function check for null and do a FindObjectOfType(typeof (YourObject) ) to grab it again if it’s missing. That will grab the reference to the newly instantiated item.

We're not calling DoNotDestroy anywhere, but I'll try the solution you suggested;that sounds like it might work. I'll let you know if it does. Thank you for the suggestion!

Alternative is to put the check in the Awake() function of the object you are loading and have it self-attach to the variable.

I'm still mystified why it was happening, but placing the check in the update function like you said and then reattaching it if it was null did the trick. (Seems simple now that I think about it... ah, hindsight and all that, no?) Thank you so much for the help! :-)

do you have any other destroy commands in the game which may be finding the wrong GameObject?

No, I don't think we ever call Destroy in this game. I'll double check, but I doubt that's what was causing it.

I went through this in last few hours, it was driving me crazy.

Then I realized I had a few “static” references to gameobjects in the gameplay scene which survive on reloads, but of course it points to a resource that has been allocated somewhere else on reload.

Check out if you have some other gameobject holding a static references to the gameobject that is null the second time you load the scene and clean it up
(it is very common to use static refs to things in the scene inside a “singleton GameManager”, careful with that).