C# 单例实例';会话管理器&x27;已经被摧毁了。返回空值

C# 单例实例';会话管理器&x27;已经被摧毁了。返回空值,c#,unity3d,C#,Unity3d,我正在做一个有趣的游戏项目,我遇到了一个为我的对话系统工作的单身汉的问题,当我用对话系统加载场景时工作正常,但如果我改变场景,然后返回[singleton]实例“ConversationManager”,它已经被破坏了。返回null /// <summary> /// Access singleton instance through this propriety. /// </summary> public static T Instance { get

我正在做一个有趣的游戏项目,我遇到了一个为我的对话系统工作的单身汉的问题,当我用对话系统加载场景时工作正常,但如果我改变场景,然后返回[singleton]实例“ConversationManager”,它已经被破坏了。返回null

/// <summary>
/// Access singleton instance through this propriety.
/// </summary>
public static T Instance
{
    get
    {
        if (m_ShuttingDown)
        {
            Debug.LogWarning("[Singleton] Instance '" + typeof(T) +
                "' already destroyed. Returning null.");
            return null;
        }

        lock (m_Lock)
        {
            if (m_Instance == null)
            {
                // Search for existing instance.
                m_Instance = (T)FindObjectOfType(typeof(T));

                // Create new instance if one doesn't already exist.
                if (m_Instance == null)
                {
                    // Need to create a new GameObject to attach the singleton to.
                    var singletonObject = new GameObject();
                    m_Instance = singletonObject.AddComponent<T>();
                    singletonObject.name = typeof(T).ToString() + " (Singleton)";

                    // Make instance persistent.
                    DontDestroyOnLoad(singletonObject);
                }
            }

            return m_Instance;
        }
    }
}


public void TriggerConversation()
{
    ConversationManager.Instance.StartConversation(conversation);
}
//
///通过此权限访问singleton实例。
/// 
公共静态T实例
{
得到
{
如果(m_关闭)
{
Debug.LogWarning(“[Singleton]Instance]”+typeof(T)+
“'已销毁。返回null');
返回null;
}
锁(m_锁)
{
if(m_实例==null)
{
//搜索现有实例。
m_实例=(T)FindObjectOfType(typeof(T));
//如果一个实例尚不存在,请创建新实例。
if(m_实例==null)
{
//需要创建一个新的游戏对象来附加单件游戏。
var singletonObject=新游戏对象();
m_Instance=singletonObject.AddComponent();
singletonObject.name=typeof(T).ToString()+“(Singleton)”;
//使实例持久化。
DontDestroyOnLoad(singletonObject);
}
}
返回m_实例;
}
}
}
公共会话()
{
ConversationManager.Instance.StartConversation(会话);
}

您需要确保未命中“装载时销毁”。你的逻辑有缺陷。向后看,下面的内容永远不会在场景加载时被破坏。如果要替换游戏对象,请确保其具有相同的值

 void Awake()
    {
        //Check if instance already exists
        if (instance == null)

            //if not, set instance to this
            instance = this;

        //If instance already exists and it's not this:
        else if (instance != this)

            //Then destroy this. This enforces our singleton pattern, meaning there can only ever be one instance of a GameManager.
            Destroy(gameObject);    

        //Sets this to not be destroyed when reloading scene
        DontDestroyOnLoad(gameObject);
}

你需要确保不要在被击中时摧毁。你的逻辑有缺陷。向后看,下面的内容永远不会在场景加载时被破坏。如果要替换游戏对象,请确保其具有相同的值

 void Awake()
    {
        //Check if instance already exists
        if (instance == null)

            //if not, set instance to this
            instance = this;

        //If instance already exists and it's not this:
        else if (instance != this)

            //Then destroy this. This enforces our singleton pattern, meaning there can only ever be one instance of a GameManager.
            Destroy(gameObject);    

        //Sets this to not be destroyed when reloading scene
        DontDestroyOnLoad(gameObject);
}

这是一个老问题,但最近在使用完全相同的单例脚本时遇到了同样的问题。我面临两种情况,这是行不通的

您不能在上使用
[ExecuteInEditMode]
属性 类,说明您正在实现此单例模式

因此,删除类上的该属性,并将编辑器逻辑移动到另一个非单例类

您正在使用Singleton实例化一个游戏对象,并且正在调用 该类来自非单行为类


这是行不通的,删除非MonoBehavior类上的单例调用。

这是一个老问题,但最近使用完全相同的单例脚本遇到了同样的问题。我面临两种情况,这是行不通的

您不能在上使用
[ExecuteInEditMode]
属性 类,说明您正在实现此单例模式

因此,删除类上的该属性,并将编辑器逻辑移动到另一个非单例类

您正在使用Singleton实例化一个游戏对象,并且正在调用 该类来自非单行为类

这是行不通的,删除非MonoBehavior类上的Singleton调用