Y
Y
Yura Mailler2020-09-22 07:57:32
Unity
Yura Mailler, 2020-09-22 07:57:32

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 answer(s)
F
freeExec, 2020-09-22
@freeExec

1) Either have an object with a flag DontDestroyOnLoadand 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 question

Ask a Question

731 491 924 answers to any question