My game is keeping track of a high score. I want this score to display when the scene changes to the win Screen. I'm a bit confused as to how to go about doing this. I have the DontDestroyOnLoad line in my script, but how would I go about actually calling the variable into my new scene? Since it's a new scene, the script doesn't actually exist in that scene yet. Would I have to copy and paste the script into the assets folder in the new scene or would I have to call if through code, and if so, how?
function Start() {
DontDestroyOnLoad(gameObject);
}
What that does, is whenever you load a new level, the object (and its scripts) won't be destroyed.
There are other ways to do this though. You could have a script like this:
// I'm going to pretend this scripts name is MyScript
static var score : int = 100;
function OnGUI() {
GUILayout.Label("Your Score: " + score);
}
Then in another script you could do something like this
function KillEnemy() {
++MyScript.score;
} // Award the player 10 points
Because score is a static variable, there is only one instance of that particular variable. You don't need a class instance to access that variable, it can be accessed from anywhere just by using the ClassName.variable syntax.