- Home /
Do global variables reset when you restart a scene?
I have a small script that uses the function Application.LoadLevel("Level") when a collision happens. This is working, however the room does not reset properly (like when I start it for the first time).
Is this due to the fact that I set some static variables when I start the level? Or do they reset as well?
If so, how would I get around this?
Answer by whydoidoit · Jan 02, 2013 at 05:11 PM
Your static variables are static for the execution of the game, they will not be reset by a level load. In general be very careful using static variables and try to consider other possibilities unless you are sure you know what you are doing.
Often a good way of providing static variables for a scene that are reset on load is to store them in a script and have that script set a reference to itself in a static member on Awake()
var variableToAccessFromSomewhere : int;
var another : String;
static var instance : NameOfThisScript;
function Awake()
{
if(instance == null)
instance = this;
}
function OnDestroy()
{
if(instance == this)
instance = null;
}
Then from another script:
NameOfThisScript.instance.variableToAccessFromSomewhere++;
Your answer
Follow this Question
Related Questions
Can someone help me fix my Javascript for Flickering Light? 6 Answers
Setting Scroll View Width GUILayout 1 Answer
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
How Do I Activate My Scripting? 1 Answer
Instantiating a random dropped consumable item from many cloned objects 1 Answer