How to pass data between scenes in Unity

后端 未结 5 1366
误落风尘
误落风尘 2020-11-22 08:30

How can I pass score value from one scene to another?

I\'ve tried the following:

Scene one:

void Start () {
    score = 0;
          


        
5条回答
  •  孤独总比滥情好
    2020-11-22 08:59

    Besides playerPrefs another dirty way is to preserve an object during level loading by calling DontDestroyOnLoad on it.

    DontDestroyOnLoad (transform.gameObject);
    

    Any script attached to the game object will survive and so will the variables in the script. The DontDestroyOnLoad function is generally used to preserve an entire GameObject, including the components attached to it, and any child objects it has in the hierarchy.

    You could create an empty GameObject, and place only the script containing the variables you want preserved on it.

提交回复
热议问题