- Home /
Restarting scene not working properly
Hello,
I have a scene in my project. If I use my 'Restart' button to reload the scene, it works partially because there are many functions or variables that do not start again.
What to do do make restarting a scene behave like I would start it the first time ?
That sucks.. Indeed the static variables fail to reset... Anyway I can hardcode that ? Telling them to reset ?
Well there are two ways:
Don't use static variables. Sometimes they are useful but they often make more problems then they help. They should only be used if really possible.
Reset the necessary varibles in the OnLevelWasLoaded() function.
Since there aren't too many, but they are referenced a lot, the second option sounds good.
Can you tell me, OnLevelWasLoaded() function, in what script (and where in the script) should be put ?
It should be the first script that the games starts to interpret ?
You can put the OnLevelWasLoaded function in any script that exist from the start of the scene. So not one that is attached to a gameobject that's instantiated later.
It can be added like any other function:
I'm gonna make one of my comments to an answer so that you can accept it to mark the question as solved.
I have it connected to my main camera. Is it good enough ? I'm asking because it is still not working...
Answer by ExTheSea · Jun 02, 2013 at 04:12 PM
Normally if you reload a scene using Application.LoadLevel all function should work propery except of static function and static variables. They don't change when reloading the level as there is only one instance per class.
Working perfectly without any need of OnLevelWasLoaded(). It seems by some beginners luck , I did set my static variables in the Start() function which seems to be enough :)
$$anonymous$$y problem was that I was setting time.scale to 0 when I was resetting the game and it was still set like this when the scene restarted.
Your answer
Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Unity 3d(Space) Questions 1 Answer
Scene changing 1 Answer
Reset The Scene ? 2 Answers