且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Coin Persist 脚本无法正常工作

更新时间:2023-12-05 18:33:58

如果您只是检查计数并删除,您目前无法控制 ScreenPersist 游戏对象的哪个实例被销毁.维护 DontDestroyOnLoad() 对象的单个实例的正确方法是这样的:

You currently have no control over which instance of the ScreenPersist GameObject gets destroyed, if you simply check the count and delete. The correct way to maintain a single instance of a DontDestroyOnLoad() object is like this:

 public static ScreenPersist m_screenPersist;
private void Awake()
{
    DontDestroyOnLoad(gameObject);
    if (m_screenPersist == null) m_screenPersist = this;
    else Destroy(gameObject);
}

理想情况下,您不希望将硬币拾取器作为 ScreenPersist 游戏对象的子项.***有一个游戏管理器更优雅地处理它,最后将您需要保留的任何数据写入 ScreenPersist 类,或写入可以用作 DontDestroyOnLoad GameObject 的同一个 GameManager 类

Ideally, you wouldnt want to have the coin pickups as a child of the ScreenPersist GameObject. Better to have a game manager that takes care of it more elegantly, and finally writes any data that you need to persist, into the ScreenPersist class, or into the same GameManager class which can function as a DontDestroyOnLoad GameObject