- Home /
Application.LoadLevel not Resetting
I have a simple button that's intended to restart the level.
The player starts in the level "Arcade" and when dies, goes to "LocalHighScore" where it shows the scoreboard and he is given two options: quit or Again!.
The button for again is thus:
if (GUI.Button(Rect(Screen.width *.7,Screen.height * .6, 128,64),"Again !"))
{
Application.LoadLevel("Arcade");
}
It loads the level, but nothing is reset. If I had to manually reset every variable to it's initial setting I'd never stop forgetting one or two. According to the reference everything is scrapped and rebuilt unless you use DontDestroyOnLoad--and I never have. For anything. I'm a little confused as to why this happens.
(The actual effect is after reloading the level the player can't move, the timer stops, their health is negative, nothing spawns. Opening the menu is broken)
Answer by Bunny83 · Oct 27, 2011 at 06:56 PM
I guess you use static variables? Now you know why you should avoid static variables because they don't belong to any object instance and therefore will never be reset. Work with objects and references instead of static stuff. The only other way is to (re)set them manually in a function.
If you don't use static variables (but i guess you do) i have no idea what's going wrong.
Ugh, that's terrible. I'm probably going to have to rewrite a ton of my code for that. No one through that warning out there before.
When using static variables you just have to assign the static variable value inside the Start() or Awake() function.
Link for more explanation: http://www.41post.com/3424/program$$anonymous$$g/using-static-variables-in-unity3d
This might be a silly question, but is using objects and references more slow/error prone than using static variables?
Answer by warddav16 · Jun 06, 2012 at 07:32 PM
A good work around is to use a singleton pattern instead of static variables.
myClass : MonoBehaviour {
private static myClass mInstance;
public static myClass Instance() {return mInstance;}
void Awake()
{
if(!mInstance) mInstance = this;
}
//All you variables go here. This will ensure there is only one type of an object at a
//time, but will still destroy and clear out your variables in every scene
//To access the variables, in your other scripts you will say
//myClass.Instance().myPublicVariableOrFunction
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613023311im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Restart Problem 1 Answer
Level Not Loading After Screen Fade 1 Answer
Can't go back to the game scene from main menu for the second time 2 Answers
How to load (restart) last scene 2 Answers
Resetting a Scene 2 Answers