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 GameObject
s, 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.