Save Scores In Scene

Hi

I have a scene where I collect coins and would like to save scores.

I created an object called Scores and placed a script in it called Scores.js. Any suggestions?

//Scores.js
var menuSkin : GUISkin;
var Score = 0;

function Awake() {
   Score = 0;
}

function OnGUI () {
   GUI.skin = menuSkin;
   text = "Score: "+Score; 
   GUI.Box (Rect (300,10,322,50), ""); 
   GUI.Label (Rect (330, 18, 100, 30), text);
}

function AddScore()
{
    Score++;
}

//Script to update score when coin is touched
var scriptName : Scores;

function OnTriggerEnter( other : Collider ) {
    if (other.tag == "Coin") {
    scriptName.AddScore();//Add score here

        Destroy(other.gameObject);
    }
}

You can use the `PlayerPrefs` class to store values between sessions. If you only want to store it between levels, you can use the `DontDestroyOnLoad` method.

This has been discussed a few times before: http://answers.unity3d.com/questions/3865/how-can-i-save-and-load-a-players-position, http://answers.unity3d.com/questions/5736/scene-to-scene-variables

player
var Scores : float = 0.0;
var skin : GUISkin;

function Start ()
{
    //it will automotically load the last value.
    Scores = PlayerPrefs.GetFloat("Coin");
}

function OnGUI ()
{
    GUI.skin = skin;
    GUI.Label (Rect(330, 18, 100, 30), "Scores: " + Scores);
}

function OnTriggerEnter (other:Collider)
{
    if (other.tag == "Coin")
    {
        var coin = other.GetComponent(coinScript);
        AddScore(coin.value);
        Destroy(other.gameObject);
    }
    //it will save automatically on save points.
    if (other.tag == "SavePoint")
    {
        //it will automatically check if this value is more then the previous
        if (PlayerPrefs.GetFloat("Coin")<Scores)
        {
            PlayerPrefs.SetFloat("Coin", Scores);
        }
    }
}

function AddScore (value : float)
{
    Scores += value;
}

coin

var value : float = 1.0;
var animation : Animation;


bla bla bla
i think that you know how to do.

Hi. I have the same problem. I want to save a game: position and score (a variable in a .js script). How can I do ? Tnx!!

player
var Scores : float = 0.0;
var skin : GUISkin;

function Start ()
{
    //it will automotically load the last value.
    Scores = PlayerPrefs.GetFloat("Coin");
}

function OnGUI ()
{
    GUI.skin = skin;
    GUI.Label (Rect(330, 18, 100, 30), "Scores: " + Scores);
}

function OnTriggerEnter (other:Collider)
{
    if (other.tag == "Coin")
    {
        var coin = other.GetComponent(coinScript);
        AddScore(coin.value);
        Destroy(other.gameObject);
    }
    //it will save automatically on save points.
    if (other.tag == "SavePoint")
    {
        //it will automatically check if this value is more then the previous
        if (PlayerPrefs.GetFloat("Coin")<Scores)
        {
            PlayerPrefs.SetFloat("Coin", Scores);
        }
    }
}

function AddScore (value : float)
{
    Scores += value;
}

coin
var value : float = 1.0;
var animation : Animation;

bla bla bla
i think that you know how to do.