- Home /
Problem when loading a scene for the second time.
My game currently has two scenes.
Scene 1 is the Main Menu, Scene 2 is the level
When my game starts it loads Main Menu, I click to start the game, then it loads the level.
When the level starts, it spawns a bunch of objects, you play the game while its spawning etc. . . . All the while a timer is counting down.
When the timer reaches zero, I have the timer script push me back into the Main Menu using
Application.LoadLevel(0);
The game then returns to the Main Menu.
My problem is this: After the game returns to the Main Menu, if I click to start my level again, the level loads, the timer begins decaying down to zero, but none of my objects respawn (I can't play the actual game).
All that happens the second time the level loads is the timer counts down and then returns me to the main menu.
Where should I look to find what is causing this problem? To me it seems that once the scene is loaded again, it should simply behave the way it did the first time it was loaded. Why wouldn't this be happening?
Answer by Eric5h5 · May 25, 2010 at 10:25 PM
If you're using any static variables, get rid of them and use regular variables instead. Either that or you have to reset them manually.
I added lines to the script at the end of the timer to reset those static variables to their original values before it returned me to the main menu. It worked :)
A question for Eric5h5, isn't static vars used for sharing them accross all scripts? If I use regular vars, how do I tell main/game$$anonymous$$anager that an item is already pickep up?
@Hakimo: No, static variables are for when you want static variables. A static variable means there's only one instance per class. If a variable is public, then anything else can access it. http://unity3d.com/support/documentation/ScriptReference/index.Accessing_Other_Game_Objects.html
@Eric5h5: Ah I see, thanks again. But it can be shared accross all instances yea? I initially thought that static is used to declare a global variable for Unity. But then again all variable is global unless I declare it inside a function right? Totally misread the 3D platform tutorial. Sorry, one last question as I'm still a bit confused, What are the differences between a static variable and a regular one in terms of behaviours?
@Eric5h5: Sorry, I forgot to add something. This page says static is used to declare a global variable : http://unity3d.com/support/documentation/ScriptReference/index.$$anonymous$$ember_Variables_26_Global_Variables.html. Does it behave like this only in unity or? Cause my initial understanding of static variables is that they only have one value throughout runtime but after learning unity, it behaves differently...sorry I'm really confused :(
Answer by hersheys72 · Nov 21, 2016 at 12:39 AM
For me I was using static events, which even though everything that was subscribing was unsubscribing it (and being destroyed) - it was still referencing the first instance of it from initial scene (which no longer existed!!)
Make sure you put void OnDestroy() { EventName = null; }
for every static event in that script.
Dear hersheys72,
Thank you very much for this comment made in 2016!!! I had exactly this issue and as I faced it for the first time I just didn't understand what is happening. Loading battle screen additively for the first time - everything is working fine. But for the second time - everywhere are errors, that object has been destroyed however everything is on place. I googled but was not able to find any clue. Thanks to your comment I found out that this was exactly events causing the troubles and I was able to fix it. Thank you kind stranger!
Answer by Tetrad · May 25, 2010 at 11:12 PM
Do you have any objects that have DontDestroyOnLoad called on them?
I do not have any DontDestroyOnLoad objects. I do have one static variable, not quite sure why this would mess things up. I'm going to try to figure out how to 'reset them manually.'
@Ecnalyr: static variables mess things up because they retain their value always. Reset them by explicitly setting the value in Start or wherever, or better yet don't use them unless you have a good reason for it....
Answer by Jaakk0S · Jan 29, 2021 at 06:35 AM
I found this old thread healpful and found out I had a static variable issue. The lesson here is that the lifecycle of static variables is the application and not the scene, and this is why static variables in MonoBehaviours need to be initialized in Awake() or Start(). If a static variable is not initialized in one of those places, it will get initialized only on class load (first time the class is referenced during the whole application lifecycle), and if the MonoBehaviour is reloaded (when a new scene is loaded), the variable will retain whatever value it had when the old scene was unloaded as the class is not reloaded.
Your answer
