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.

What went wrong with your statics?

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. [9410-teststatics.unitypackage.zip|9410]

2 Answers

2

First option is to use PlayerPrefs:

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:

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.

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