Keeping Scores in Unity and pass to next scene

前端 未结 2 1882
滥情空心
滥情空心 2020-12-22 00:04

After reading and trying many different examples, I am still stuck with this fairly simple problem on getting a score from one level to the next in Unity.

I have a

相关标签:
2条回答
  • 2020-12-22 00:47

    The DontDestroyOnLoad method is callable on an object, not on a single variable. It represents the best possible approach for a value of that kind, that needs to be "exported" from your actual scene.

    However, you can also use PlayerPrefs. They are intended for informations at a "higher" level of persistency, like flags for unlocked levels, final scores for a ranking system, or attributes of a customizable character. That's why the DontDestroyOnLoad way is better suited (for your "temporary" score), but you can use this one also. Basically, PlayerPrefs are information stored in the registry of your operative system.

    Before exiting from the actual scene, save the score this way:

    PlayerPrefs.SetInt("Player Score", finalScore);
    

    And, when next scene starts, initialize score's variable by retrieving that information:

    finalScore = PlayerPrefs.GetInt("Player Score");
    

    (here a similar post)

    0 讨论(0)
  • 2020-12-22 00:53

    Someone else said, and I shamelessly quote, use a static variable for your data in a C# class, and there you go...

    (Still I'd prefer to pass parameters to LoadLevel itself, but that's another story)

    0 讨论(0)
提交回复
热议问题