Load a score from one scene to another scene

Hello, I have a score manager script that is attached to my score text in my hierarchy.
In my first scene the score works fine by killing enemies. how to i send the score from the first scene to the second scene to continue to increase my score. i tried DontDestroyOnLoad but didn’t work. How do i use it to update my score.

here is my score script.

using UnityEngine;
using UnityEngine.UI;
using System.Collections;

public class ScoreManager : MonoBehaviour
{
public static int score;

Text text;

void Awake ()
{
    text = GetComponent <Text> ();
    score = 0;
}

void Update ()
{
    text.text = "Score: " + score;
}

}

Making it static should make it persist through scenes. PlayerPrefs is a usual method but it is insecure.

how do i add PlayerPrefs method to the script? Can you assist me with some bonding. Thanks

@meat5000 sir why PlayerPrefs is insecure. i use it all the time ? :|

When i add the deletekey it's giving me an error

where did you add the deletekey

1 Answer

1

This should help you, Unity - Scripting API: PlayerPrefs you can use the player prefs to save the score and then make a script to load it when you load the next scene.

I read about the PlayerPrefs. But how do i use that in my score script.

Text text; void Awake () { text = GetComponent <Text> (); score = PlayerPrefs.GetInt("Score"); } void Update () { text.text = "Score: " + score; PlayerPrefs.SetInt("Score", score); } That "Should" work to set the value of the player pref to the same value of your score, if that does not work then try changing Awake to Start

That was just perfect. it worked thanks a lot.

How do i rest the score back to 0 when its gameover and i want to start again.

just put this in your script where you want to delete the score public static void DeleteKey(int Score); that should work if i recall correctly.