- Home /
Objects are destroyed/disfunctional after changing scene
http://imgur.com/XrrC7FR Bassicly whenever the pinball goes down to low, it hits a trigger (the deadzone) that resets the position of the ball, and respawns all the coins. Now all the coins are childs of a parent called CoinParent that has a script that handles detecting the amount of coins gotten, and also handling respawning all the coins. Now this all works totally fine IF i havent changed scene. BUT whenever i change scene, any refference in the CoinsParent script (CountCoins) to: transform, or gameObject will give me the following error:
MissingReferenceException: The object of type 'countCoins' 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.
Now it get's weirder. Because even if the CoinParent GameObject is not active, when the ball hits the deadzone and all the coins are supposed to reset, it gives me the excact same error, and that error when i click on it, takes me to the same place that i was taken when the CoinParent Gameobject WAS active. It's like it litteraly doesn't care if it's active or not.
I really have no idea how to fix this. it says the countCoin script is destroyed but it's clearly executing. also - yes - of course i have checked if either the gameObject or the transform are null in the script but that ALSO gives me the error, cause the script simply can't handle any refference to one of those two
please check if your script is attached to any other object by mistake
Any suggestions for practical ways of doing this that don't involve manually checking trough each and every object?
To see which objects have your component attached, stick this in your awake or start function:
Debug.Log("running code for object" + this.name);
this.name should show you the name of the gameobject it is attached to.
without seeing your code: I'm not sure I understand the issue, but here is some possibly relevant information: When you initially define the scene that you are changing TO, in the editor, does it include a CoinParent Object(I would think it should)? Assu$$anonymous$$g so, THIS (second scene object) is the CoinParent object you need to reference in your scripts after you change the scene. From the error, it looks like the CoinParent object in the first scene is destroyed, when the new scene is loaded, (and referencing a destroyed object is obviously NOT going to work).
Yup. This would be my guess too. $$anonymous$$aybe you have a script with DontDestroyOnLoad or a static variable that references coin counter in the first scene. After loading a new scene, that script is still trying to reference the coin counter in the first scene.
thing is though, the problem doesn't seem to be other scripts trying to use the countCoins script. It's all done with events, no individuel objects are targeted
Answer by aWolfKing · Mar 06, 2016 at 11:35 AM
http://docs.unity3d.com/ScriptReference/Object.DontDestroyOnLoad.html Hope this helps, i never used it but it might solve your problem.
Answer by biebuster · Mar 09, 2016 at 10:05 AM
Sadly this seems to make the problem even worse... Either the object is spawned once every frame, or it's not destroyed or spawned but EVERYTHING else than the object is destroyed. Really weird stuff
Answer by younes2234 · Apr 27, 2020 at 12:11 PM
Hi, this is generaly due to a wrong way of initializing variables ... just make sure that all your gameobject variables or any other local variables are intialized inside Start or awake , your problem doesn't require using dontdestroyonload .