- Home /
save variable from scene to scene
How to pass variable's from scene to scene in a ordinary c# script/class without it being inherited by monodevelop?
any ideas?
I attempted to make the variables static but no go.
The static were wiped. If I set a boolean to true in a scene when loading the other scene its auto sets to false. With no code telling it to do that.
we do use DoNotDestroyOnLoad but that does not help.
Was the thing in the static a reference to a component or game object? If it was then it's that which needs to be DontDestroyOnLoad.
Statics are definitely not reset between scenes - something you are doing must be setting the value.
I attach a demonstration. Load the TestScene - its sets a static and then loads another and displays the value which remains set.
Answer by gribbly · Mar 27, 2013 at 07:28 PM
First option is to use PlayerPrefs:
http://docs.unity3d.com/Documentation/ScriptReference/PlayerPrefs.html
PlayerPrefs.SetFloat("SaveThis", 3.0f"); //save a var called "SaveThis" as 3.0f
float f = PlayerPrefs.GetFloat("SaveThis"); //f = 3.0f
if(PlayerPrefs.HasKey("SaveThis")) { //test a key exists before using it }
Another option is to call DontDestroyOnLoad() on a game object in your first scene:
http://docs.unity3d.com/Documentation/ScriptReference/Object.DontDestroyOnLoad.html
There are other options too, but these are the simplest.
DontDestroyOnLoad does not work as per my post. But will look into the other answer you gave.
Answer by Michellia · Apr 01, 2013 at 01:03 PM
So we tried everything.
We have two scene
The first scene makes a static instance of it self and a static instance of a class the is the child of the first instance.
Both have variables and the instances are static. When we load another scene we make a static instance of itself.
All three are only initialized once. we you debug.log to verify this.
But all the variables in the first scene the parent and the child are all reset to false or 0 what ever the type of variable is.
WE are trying to preserve mouselook to true and other variables.
but mouse look goes t false along with all booleans and all int go to 0, etc...
thansk all
Your answer
![](https://koobas.hobune.stream/wayback/20220613095213im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Saving variables? 3 Answers
Saving for a Level Editor 1 Answer
Don't save gameobjects to scene file (No hide flags) 0 Answers
Make variable to select scene? 2 Answers
Passing position to function 1 Answer