0

I have a simple script to count up a score for the player, how can I take this value and display it on a game over scene?

This is my score script:

public class scoreScript : MonoBehaviour {

 public static int scoreValue = 0;
 Text score;

 // Use this for initialization
 void Start () {
     score = GetComponent<Text>();
 }

 // Update is called once per frame
 void Update () {
     score.text = "Score:" + scoreValue;
 }

}
Shayki Abramczyk
  • 36,824
  • 16
  • 89
  • 114
sam austin
  • 23
  • 5

4 Answers4

1

Simple, just call “scoreScript.scoreValue” in any script you want in game over scene

Jana7i
  • 11
  • 1
1

Store it in PlayerPrefs while you exit current scene save it in playerpref like :

PlayerPrefs.Setint(" CurrentScore",scoreValue);

then retrive it in new scene by :

scoreValue = PlayerPrefs.Getint(" CurrentScore");
derHugo
  • 83,094
  • 9
  • 75
  • 115
Awais Chaudhry
  • 47
  • 1
  • 1
  • 9
0

You already made it public static, all that's left is to write in your game over scene's text field script:

gameOverText.text = "Score: " + scoreScript.scoreValue;

assuming that gameOverText is a Text.

You don't need to create an instance to access a static member, you should access them using a class name (scoreScript in your case).

However, it's not good to mix storing global score and a textField for displaying it in a single class, because as you add new features to the game, all global variables will be in different classes, and you will pay increasingly more attention while modifying your code. To avoid this, you may use a static class as a "core" where you store all global variables. For the first time, this will do.

DontDestroyOnLoad works with GameObjects, so they will not destroyed when a new Scene is loaded. If you call it in your script, your score counting GameObject will remain, and so will a text field because it's a compoment of that object, and will be present on game over scene, so don't do it.

trollingchar
  • 760
  • 2
  • 6
  • 18
  • i think i added it to the code correctly and it compiled with no errors but it doesnt seem to do anything any tips? – sam austin Jan 06 '19 at 22:39
  • Your script for game over label should be enabled and be on an enabled GameObject. You may add `Debug.Log("...");` in it to see if the function was called. – trollingchar Jan 07 '19 at 15:01
  • im getting an error saying i havent called the script score variable, how do i actually instantiate it? – sam austin Jan 12 '19 at 15:40
-2

When you are loading a new scene, the gameObject that carries the "scoreScript" is lost. In order to transfer the same gameobject to a new scene (which makes sense in order to not lose your information that the script is carrying), you need to use the "DontDestroyOnLoad" method. You just call the method in the Awake method of your monobehaviour, and then your gameobject (together with the data on your script) will persist new scene loading. Here is the relevant documentation: https://docs.unity3d.com/ScriptReference/Object.DontDestroyOnLoad.html

and here is a code sample:

public class scoreScript : MonoBehaviour {
    public static int scoreValue = 0;
    Text score;

    void Awake(){
        DontDestroyOnLoad(this.gameObject);
    }

    void Start () {
        score = GetComponent<Text>();
    }

    void Update () {
        score.text = "Score:" + scoreValue;
    }
}
konsfik
  • 97
  • 4
  • 2
    Also, using a static value for score keeping makes global access easy, but you may want to consider the use of events for updating your score value (i.e. when something happens an event is raised and the score manager responds to that event by increasing the score) This way you will also not need to always repeat the "score.text = "Score:" + scoreValue;" in the Update method – konsfik Jan 06 '19 at 15:13