- Home /
Saving variables?
Hello, I am trying to display the player's score (which I have as a static variable) from one scene to the next. I am not taking about player prefs. How can I do this? I know about
function Awake () {
DontDestroyOnLoad (transform.gameObject);
}
but from what I can see, that only works on entire game objects, I just want to save one script (and not the whole game object). Thanks.
Answer by Tyler 2 · Dec 01, 2010 at 02:55 AM
Its a static var, so if I call from another scene, its fine.
Answer by Statement · Dec 16, 2010 at 01:38 PM
A simple example on how to use static variables to maintain data that lives through level loads would be to use this script on a new game object and have it be dedicated to keep Static Variables. That way nothing else is kept running between levels (such as the player, reading a comment you wrote earlier).
GameVars.js
static var score : int = 0;
function Awake() { gameObject.name = "Static Variables"; DontDestroyOnLoad(gameObject); score = 0; }
And then you can access the data simply in another script:
GameVars.score += 500;
Answer by Lance · Dec 01, 2010 at 12:10 AM
Why not have your score be a static
class variable and access that from your script regardless of its GameObject
?
How do I do that? And, the object is attached to the player object, I want to destroy the player object at the end of the scene.