How to write in UItext glasses scored in another scene? That is, write a script variable in UItext in another scene.

  • Didn’t they advise you to make a class with strike variables? - Alexey Shimansky
  • Thanks, advised. Why it does not work. Declared like this: public static int score = 0; Now I need to get this variable in another scene. To: mycount.text = score.ToString (); It is not visible in another scene. - Georgich

2 answers 2

Some do the following: create a separate prifab for everything for the whole game. It can store glasses, life, some textures, graphics, music, etc., which is common to all scenes. Call this pribab, for example GameManager . And then they drag this prifab from scene to scene using the DontDestroyOnLoad function - this means that the object is not deleted when the new scene is loaded.

The code looks like this:

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

To find out if a new scene has been loaded (with the exception of the default zero), you can use the OnLevelWasLoaded method

As a result, there will be a GameManager GameManager in it with a script with everything you need, let's say the GameManagerScript will also be called, in which there will be a field with points and other necessary and DontDestroyOnLoad and OnLevelWasLoaded .

During the game, you can either add points to that script immediately, or, when moving to another scene, take points from another script and enter the Game Manager in the variable points .

On the next scene in the OnLevelWasLoaded method, OnLevelWasLoaded write instructions to take the value from the variable points and put it where you need it, in the same UI.text, for example.

Like that:

 using UnityEngine; using System.Collections; using UnityEngine.UI; public class GameManagerScript: MonoBehaviour { public static int points = 100; void Awake() { DontDestroyOnLoad(transform.gameObject); } void OnLevelWasLoaded(int level) { Text ui = GameObject.Find("/Canvas/Text").GetComponent<Text>(); Debug.Log("scene was loaded"); } } 

It is advisable to make this script singlton in order to avoid duplication. Something like this:

 using UnityEngine; using System.Collections; using UnityEngine.UI; public class GameManagerScript: MonoBehaviour { public static GameManagerScript instance; public static int points = 100; void Awake() { if (!instance) { instance = this; DontDestroyOnLoad(transform.gameObject); } else { Destroy(gameObject); } } void OnLevelWasLoaded(int level) { Text ui = GameObject.Find("/Canvas/Text").GetComponent<Text>(); Debug.Log("scene was loaded"); } } 
  • Thanks for the explanation and the code. Zero scene - the beginning of the game. The start button transfers to the next scene, where the score is actually kept. Prefab should be created here? And hang a script on it with counting, right? I make it a separate script DontDestroyOnLoad. Either I did not understand it, or I did it crookedly. Does not work. - Georgich
  • @Georgich It is necessary to create an empty object, make it a prefab ... write this script .. throw it at once on that prefab and you can immediately throw at the zero stage and then he himself will pick up in the following scenes ....... What does it mean? Any errors? ...... a visual video of youtube.com/watch?v=NY2WB28I_X8 - Aleksey Shimansky
  • Thank you. It seemed to me that the video switches the scenes with buttons, and everything seems to be all. - Georgich
  • about the code, no errors. It just doesn't work. It does not transfer anything to another scene. I hung the script on the Empty with an account so that the account was available in another scene. I hung your script on it and after that I made it prefab. I put the prefab in both scenes. Nothing is transferred. - Georgich
  • @Georgich on both scenes do not need to throw anything ... ONLY on the original, where the game begins .......... in the video it is available that shows SceneManager (if you pay attention - it is not on the second scene) and it transferred from the first scene to the second when you click on the button ..... - Alexey Shimansky

Just want to say that Unity3d already has a singleton wiki. To avoid cycling: W