- Home /
Static variable reset at LoadLevel
I do not understand why my static variables are reset to scene change.
question: If I take a variable on scene A, from an object like this:
StaticData.turretSlot01 = Weapons.FindChild ("_ TurretSlot_3D_01").GetComponent<SS_ShipTurretSlot>();
Where turretSlot01 is obviously static. If there is no Weapons transform in the scene B, the variable is canceled?
Answer by nonathaj · Apr 12, 2016 at 03:07 PM
Your static variable inherits from UnityEngine.Object and is a part of the scene, thus it is a managed Unity type. These types that are within a scene are destroyed when a new scene is loaded (and Unity actually does the management on the C++ backend and sets references to the C# version of the object as a fake null, which you can read a little bit more about here).
You will notice that the other, non-Unity, types retain their values between scene loads. You can have Unity keep an object alive between scenes by calling DontDestroyOnLoad on the GameObject or the Component itself.
DontDestroyOnLoad work with components? I can't store the gameobject, I need to use my static variables on other object in the loaded scene. $$anonymous$$new I "DontDestroyOnLoad" is applicable only to gameobjects. How do I maintain a component without a gameobject? And, static variables are not outside of the scene?Indeed iit is not necessary that it is attached in the scene. At least I knew so ;) Thus, only variables/classes of Unity are maintained on loadLevel??
Some facts:
Components can't live without a gameobject.
Components and GameObjects in the current scene are destroyed when you load a new scene.
Yes, static variables are outside of the scene, but not GameObjects and Components, they belong to the scene and are destroyed when you load a new scene. So your static variable still exists but the object it references doesn't.
DontDestroyOnLoad can be used on both GameObjects and Components. When used on a component it's the same as using it on the GameObject the component is attached to.
DontDestroyOnLoad only works on root GameObjects. So using it on a nested child object has no effect since the object gets wiped out along with the parent.
If you only want to keep a child GameObject but not the parent you have to unparent the child first by setting the childs parent property to null and then use DontDestroyOnLoad.
Thanks for the reply . I was trying to store my var with playerpref .
Answer by aditya · Apr 13, 2016 at 05:49 AM
Create an empty GameObject name it something and assign a Tag to it (must be unique) ... now create a script (for this example i m using C#) and assign it to this GameObject and i prefer to create a prefab of this GameObject so you can just Drag 'n' Drop it in all your scenes .... now in your script copy and paste this
public static YOUR-CLASS-NAME instance;
void Awake(){
if (instance == null) {
DontDestroyOnLoad (gameObject);
instance = this;
} else if (instance != this) {
Destroy (gameObject);
}
}
This is actually a singleton design pattern, now make all your static variables a member of this class and access them in any scene without values being changed