Saving score to next level?

Hey I was trying to get my score to keep as the player goes to the next level,
I tried doing DoNotDestroy but when I do that it creates two of the Players in the next level,
I’ve tried other examples but i wasn’t sure how to link the Count to a save function.

var CountText:UI.Text;
var PickupText:UI.Text;

function Start () {
	Time.timeScale = 0;
	count=0;
	pickups=0;
	CountText.text=("");
	PickupText.text=("");
	winText.text=("");
	SetCountText();
	SetPickupText();
}

function OnTriggerEnter(other : Collider) 
	{
		if (other.gameObject.tag == "Pickup")
		{
			other.gameObject.SetActive (false);
			count += 1;
			SetCountText();

		}else if(other.tag == "Exit" && pickups >=8)
			{
				transform.position = Vector3(0, 0.5, 0);
				Time.timeScale = 0;
				Application.LoadLevel("Level_02");
				
		}else if(other.gameObject.tag =="MusicPickup1")
			{
			other.gameObject.SetActive (false);
			sound1.mute = false;
			pickups += 1;
			count += 5;
			SetCountText();
			SetPickupText();
			MusicNote.SetActive (false);
			MusicNoteActive.SetActive (true);
		}

function SetCountText ()
	{
	CountText.text = "Count: " + count.ToString ();
	if (count >=100)
	{
		winText.text=("You Win!");
		Exit.gameObject.SetActive (true);
	}
}
function SetPickupText ()
	{
	PickupText.text = "Pickups: " + pickups.ToString ();
	if (pickups >=8)
	{
		winText.text=("ALl DONE!");
		Exit.gameObject.SetActive (true);
	}
}

Which other example? Did you mean PlayerPrefs (Unity - Scripting API: PlayerPrefs)? If not, maybe you can try that?

The reason why there’s a 2 Players is because the last one was carry over and the new was created when the scene is reloaded. One way of handling that is to create a static instance of that class and in the Awake function checks if the instance is null or not and initialise it to “this” if it is and destroy the game object if it’s not. Example:

void Awake() {
		DontDestroyOnLoad (this);

		if (instance == null) {
			instance = this;		
		} else {
			DestroyObject(gameObject);
		}
}

Although I wouldn’t recommend this way due to personal taste that it’s seems messy. Another way I can think of is to set the value to static, which should make the value to never change (just make sure when you’re careful when resetting it so it wouldn’t be reset unintentionally). The PlayerPref might be a cleaner way though (save it at the end of the level and load it again at the start of the next one).

  • First, set your class with
    DontDestroyOnLoad.
  • Then, when you
    load the new scene, check on Awake if
    the GameObject already exists, if it
    does, destroy the object that is
    being created.

An example:

public class YourClass : MonoBehaviour
{
    void Awake()
    {
        if (FindObjectsOfType(typeof(YourClass)).Length > 1)
            DestroyImmediate(gameObject); // Found instance of script in scene... suicide!
    }
    
    void Start()
    {
        DontDestroyOnLoad(gameObject); // First time here, never destroy
    }
}

I hope it helps.

Its easy. you just need to implement a class, say we name it Scores, and then you set it as your players static property.
here is an example to show you how its done in action:

public class Player : MonoBehaviour {
    
    public static Scores scores;
    public static bool gameInitiated = false;
    void Start()
    {

        if (gameInitiated)
        {
            scores.score += 100;
        }
        else
        {
            scores = new Scores();
            scores.score = 100;
            gameInitiated = true;
        }
    }
	
	void Update () {
        Debug.Log(scores.score);
	}

    void OnGUI()
    {
        if (GUILayout.Button("Load Next Level"))
        {
            if (Application.loadedLevelName == "scene1")
                Application.LoadLevel("scene2");
            else
                Application.LoadLevel("scene1");
        }
    }
}

public class Scores
{
    public int score;
    public int lives;
}