Loading a scene and keeping original score

Hello I’m trying to load from one scene to another while keeping the original score. The game I’m working on involves collecting coins to add to a score, over multiple scenes. I wish to go from ‘Scene 1’ to ‘Scene 2’ while keeping the score accumulated in ‘Scene 1’ to be used and added to in ‘Scene 2’.

If it helps the script I’m using for the scoring system is:

    var score = 0;
    var scoreText = "Score: 0";
    var mySkin : GUISkin;
    
    function OnTriggerEnter(other : Collider ) {
    
       if (other.tag == "Coin") {
    
            Debug.Log("Other object is a coin");
    
            score += 140;
    
            scoreText = "Score: " + score;
    
            Debug.Log("Score is now " + score);
    
            Destroy(other.gameObject);
    
        }
    
        else if (other.tag == "Rock" && score > 0) {
    
        Debug.Log("Other object is a rock");
    
        score += -100;
    
        scoreText = "Score: " + score;
    
        Debug.Log("Score is now " + score);
    
        }
    
    
    
    	else if (other.tag == "Tree" && score > 0) {
    
        Debug.Log("Other object is a tree");
    
        score += -90;
    
        scoreText = "Score: " + score;
    
        Debug.Log("Score is now " + score);
    
    	}
    
    }
    
    
    
    function OnGUI () {
    
        GUI.skin = mySkin;
    
        GUI.Label (Rect (250, 10, 200, 400), scoreText.ToString()); 

To transition from scene to scene I’m using this script, attached to a ‘Barrier’:

 function OnTriggerEnter ()
    
    {
    
    Application.LoadLevel ("Scene 2");
    
    }

I’ve tried using a DontDestroyOnLoad script which works to a certain extent. It keeps the score, and after collecting coins in ‘Scene 2’ they add to the original score.
However, upon activating the scene-transition script mentioned earlier the next scene loads with the ‘Player’ starting directly after it collided with the ‘Barrier’, which activates the scene-transition script. This is a bit of a problem as the ‘Player’ needs to start from the very beginning of ‘Scene 2’. Without the DontDestroyOnLoad script the ‘Player’ does start at the very beginning of the next scene but the score isn’t intact.

I apoligise for the long question. Thank you for any answers or feedback,
-Ben

You can make a separate object - an empty game object - and put your do not destroy script on that. Then make that object a singleton:

 static var Instance : YourScriptName;
 var score : int;

 function Awake()
 {
       Instance = this;
       DontDestroyOnLoad(this);
 }

Now you can access your score like this:

  YourScriptName.Instance.score += 100;

I did it via learning about player preferences here: http://answers.unity3d.com/questions/250734/player-prefs-saving-position-script-assistance.html

There are other methods as well, but I used player prefs to also save player profiles, and carry them over to other scenes too. it’s incredibly easy to save an integer using player prefs. Also refer to function Onlevelwasloaded() or in your function Awake to load your score in the next screen from your player pref save.

Hope this helps.

Updated the script as a copy and paste solution, so now it’s actually functional.

HERE