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
– RC-1290Just set Instantiate one object in the Start(); So each level01 it will be instantiated with the same reference.
– Vollmondumall 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.
– MountDoomTeam@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!
– inejwstine