Answer the question
In order to leave comments, you need to log in
How to pass a variable to another scene in a unit?
How to pass an int variable from one scene to another scene
public Text text;
public int Score = 0; // ========== вот эту переменную
public void Update()
{
text.text = Score + "";
}
private void OnTriggerEnter2D(Collider2D collision)
{
Score++;
text.text = Score + "";
Destroy(gameObject);
}
Answer the question
In order to leave comments, you need to log in
1) Either have an object with a flag DontDestroyOnLoad
and store all values in its components.
2) Or make a variable static
3) You can of course use and PlayerPrefs
Well, in the end, it is considered good practice to separate the logic of storage, processing and display. The conditional patron should not care about showing the player's score on the screen.
P.S. I'm not talking about the fact that your garbage collector hangs itself, collecting after each frame the created string with points, although they have not changed since the last frame.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question