Reputation: 11
I want to make a variable I can access in any unity scene.
I need this to see what level the player was in before death, so he can respawn at the level he died.
Please say the best method to do this if you can.
Upvotes: 1
Views: 1521
Reputation: 716
There might be other ways of doing this but I have got to know these two.
Either: You can create a static class like this.
public static class Globals
{
const int ScreenFadingIn = -1;
const int ScreenIdle = 0;
const int ScreenFadingOut = 1;
public static float ScreenFadeAlpha = 1.0f;
public static int ScreenFadeStatus = ScreenFadingIn;
}
Code took from this thread C# Global Variables Available To All Scenes
OR: You can create a gameobject and attach the script which contains your desired variable and make that put line DontDestroyOnLoad(this);
Like this
void Awake() {
if(Instance != null) {
Destroy(this.gameObject);
return;
}
Instance = this;
DontDestroyOnLoad(this);
}
Upvotes: 2