Score consistent between scenes?

Hello, I am making a 2D sidescroller. And I have a score script that raises the score number each time you collide with a coin (“Derps” in my game [the game is a joke, so the names for things are jokes, don’t worry about it :stuck_out_tongue: ])

Here’s the score code (it’s attached to the player):

var score = 0;
var scoreText = "Derps: 0";
var mySkin : GUISkin;

function OnTriggerEnter( other : Collider ) {
    Debug.Log("OnTriggerEnter() was called");
    if (other.tag == "coin") {
        Debug.Log("Other object is a coin");
        score += 1;
        scoreText = "Derps: " + score;
        Debug.Log("Score is now " + score);
        Destroy(other.gameObject);
    }
    if (other.tag == "redcoin") {
        Debug.Log("Other object is a redcoin");
        score += 50;
        scoreText = "Derps: " + score;
        Debug.Log("Score is now " + score);
        Destroy(other.gameObject);
    }
}

function OnGUI () {
    GUI.skin = mySkin;
    GUI.Label (Rect (10, 10, 500, 200), scoreText.ToString()); }

Now, each “level” is a different scene. Moving between scenes is triggered by colliding with an “exit” block.

Exit script:

var deathSound : AudioClip; 

function OnTriggerEnter (other : Collider) {
    if(other.gameObject.name =="Player"){
        Application.LoadLevel (1);
    }
}

What I want to do is keep the score consistent between each level (scene). When you do move from scene to scene, the score resets back to 0, when it should stay at the score you had before you exited the level.

I was thinking maybe to add some sort of code that writes the score onto another script, and the score can check to see what the score is.

But now that I think of it, the score script has this line:

Debug.Log("Score is now " + score);

maybe we can write a script that checks to see what this is (unless this is just for the editor, not for the game).

I hope this all makes sense. Thanks in advance :slight_smile:
also, if there’s a better way to be doing a score system, please say haha :slight_smile:

store the score in PlayerPrefs. search for PlayerPrefs in the script reference if you want more info

You can also make the score persistent by storing it using a static variable, or in a member variable associated with an object that’s been made persistent using DontDestroyOnLoad().

PlayerPrefs is also an option (as mentioned), but if you only need to store this value while the game is running (and not between runs of the application), one of the other options mentioned might be preferable. (You could still use PlayerPrefs to store a persistent score or a high score - you just wouldn’t use it as a means of saving the score between scenes.)