Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

关于PersistentMonoSingleton的DontDestroyOnLoad没被调用的问题 #123

Open
yyyysh opened this issue Dec 16, 2023 · 0 comments
Open

Comments

@yyyysh
Copy link

yyyysh commented Dec 16, 2023

Snipaste_2023-12-16_10-28-24
图中在其他mono脚本里调用继承了PersistentMonoSingleton的脚本时 该脚本的Awake方法还没执行,这时候从场景上FindObjectOfType得到instance
等到了该脚本Awake方法执行时 mInstance == null 导致DontDestroyOnLoad方法不会被执行 是不是把 DontDestroyOnLoad(transform.gameObject); 这句从if里提出来比较好?

  protected virtual void Awake()
        {
            if (!Application.isPlaying)
            {
                return;
            }

            if (mInstance == null)
            {
                mInstance = this as T;
                DontDestroyOnLoad(transform.gameObject);
                mEnabled = true;
            }
            else
            {
                if (this != mInstance)
                {
                    Destroy(this.gameObject);
                }
            }
        }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant