简体   繁体   English

我该如何解决MissingReferenceException问题,即当我未销毁游戏对象时会告诉我该游戏对象已销毁?

[英]How can I fix MissingReferenceException that is telling me game object is destroyed when I didn't destroy it?

I have code that looks like this. 我有看起来像这样的代码。

public static Dictionary<int, Action> functionsMap;
void Function()
{
    if (!isDictionaryInitialized)
    {
        functionsMap = new Dictionary<int, Action>();

        functionsMap.Add(1, () => StartCoroutine(Function1()));
        functionsMap.Add(1, () => StartCoroutine(Function2()));
    }
 }

void CheckForFunction()
{
    var r = currentFunctionNumber;

    if (functionsMap.TryGetValue(r, out currentAction)) { currentAction(); }
}

The code works fine when I start my program. 启动程序时,代码工作正常。 However if I go to another scene and then return to it, I get this error. 但是,如果我转到另一个场景然后返回它,则会收到此错误。

"MissingReferenceException: The object of type 'ScriptName' has been destroyed but you are still trying to access it. Your script should either check if it is null or you should not destroy the object." “ MissingReferenceException:'ScriptName'类型的对象已被破坏,但您仍在尝试访问它。脚本应检查它是否为null或不应该破坏该对象。”

The problem is I have never destroyed the object. 问题是我从未破坏过该物体。 Initially I didn't have bool isDictionaryInitialized and I defined the new Dictionary outside of the Function because I thought the error was related to my program trying to access a Dictionary that was deleted after the scene was closed. 最初,我没有bool isDictionaryInitialized ,而是在Function之外定义了新Dictionary ,因为我认为该错误与我的程序尝试访问关闭场景后删除的Dictionary有关。 I get the same problem with or without the bool, and regardless of where I define the Dictionary. 无论是否使用bool,无论我在哪里定义Dictionary. ,都会遇到相同的问题Dictionary.

What is causing this, and what is the reason so I can avoid making the same mistake? 是什么原因造成的,原因是什么,我可以避免犯同样的错误?

Edit: This question was marked as duplicate, but the link I don't believe applies to my situation, or if it does I don't understand how. 编辑:这个问题被标记为重复,但是我不相信该链接适用于我的情况,或者如果我不明白该如何做。 It says static objects are not reloaded on a scene change, and the Dictionary is defined as a static object. 它说静态对象不会在场景更改时重新加载,并且Dictionary被定义为静态对象。 I also tried changing it to non-static and the result is the same. 我还尝试将其更改为非静态,结果是相同的。

I have dozens of gameobjects in my code and don't have this issue with any other object, so I assume the problem is related to how the dictionary object is defined. 我的代码中有数十个游戏对象,而其他任何对象都没有这个问题,因此我认为问题与字典对象的定义方式有关。 Is there a way to keep the Dictionary object from being destroyed on scene change? 有没有一种方法可以防止Dictionary对象在场景更改时被破坏? I don't have it as a game object in the scene, it's just defined in the code itself as a public static Dictionary. 我没有将它作为场景中的游戏对象,只是在代码本身中将其定义为公共静态词典。 Could someone tell me what I need to do differently please and thank you? 有人可以告诉我我需要做些什么吗,谢谢?

The problem might be caused by changing scenes because simply loading another scene would destroy all gameobject in the current scene. 该问题可能是由于场景更改所致,因为仅加载另一个场景将破坏当前场景中的所有游戏对象。

According to Object.DontDestroyOnLoad . 根据Object.DontDestroyOnLoad

The load of a new Scene destroys all current Scene objects. 新场景的负载会破坏所有当前的场景对象。

To solve this, you can either use DontDestroyOnLoad function to mark the object you want to be kept before loading another scene, or using different way to load like LoadSceneMode.Additive without destroying the current scene. 要解决此问题,可以使用DontDestroyOnLoad函数在加载另一个场景之前标记要保留的对象,也可以使用DontDestroyOnLoad加载方式,例如LoadSceneMode.Additive,而不破坏当前场景。

First you are adding two Action s to your Dictionary but both with the same key . 首先,您要向字典中添加两个Action ,但是它们都具有相同的key

functionsMap.Add(1, () => StartCoroutine(Function1()));
functionsMap.Add(1, () => StartCoroutine(Function2()));

wouldn't this overwrite the first entry? 这不会覆盖第一个条目吗?


Than your general problem is: 比您的一般问题是:

While functionsMap is static the two actions / one action you added are non-static ( StartCoroutine always requires an instance of MonoBehaviour to run on) and therefore only available for the according component! 虽然functionsMapstatic但是两个动作/您添加的一个动作是非静态的( StartCoroutine始终需要运行MonoBehaviour的实例),因此仅可用于相应的组件! (You talked about an Update method so this has to be a MonoBehaviour attached to a GameObject in your first scene.) (您谈到了Update方法,因此它必须是在第一个场景中附加到GameObject的MonoBehaviour 。)

Now if you change the Scene, the GameObject holding that component is probably destroyed if you are not using DontDestroyOnLoad 现在,如果您更改场景,那么如果您不使用DontDestroyOnLoad ,则拥有该组件的DontDestroyOnLoad可能会被破坏。

The result is that your static Dictionary stays intact and filled meaning the entry with key = 1 still exists but the Value namely the added non-static Action is no longer available. 结果是您的static词典保持完整并充满,这意味着key = 1的条目仍然存在, 但是 Value即添加的非静态Action )不再可用。

To avoid that you should add 为了避免这种情况,您应该添加

DontDestroyOnLoad(this.gameObject);

either in Awake or Start to prevent the GameObject holding the component and thereby holding the Action that belongs to the reference(s) in the entry in your Dictionary. 可以在“ Awake或“ Start以防止GameObject保留该组件,从而保留属于Dictionary条目中引用的Action

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何永远销毁游戏 object? - How can I destroy a game object forever? 父级销毁时如何销毁子级对象? - How to destroy child object when parent is destroyed? 我正在努力让游戏 object 在与玩家碰撞时被摧毁 - I am struggling to get a game object to be destroyed when colliding with the player 为什么当我设置PatternType时,EPPlus告诉我“当没有设置patterntype时无法设置颜色”? - Why is EPPlus telling me that I “Can't set color when patterntype is not set” when I have set PatternType? 如何在新游戏开始时使用“Don't Destroy On Load”销毁游戏 object - How to destroy a game object with “Don't Destroy On Load” when a new game starts 如何修复“'GameObject' 类型的 object 已被破坏,但您仍在尝试访问它。” 统一 - How I can fix the “The object of type 'GameObject' has been destroyed but you are still trying to access it.” Unity 当游戏对象被销毁时出现 MissingReferenceException - MissingReferenceException when gameObject has been destroyed 如何销毁游戏 object 但在 unity3D 之后有粒子效果? - How can I destroy a game object but have a particle effect after in unity3D? 我如何让光线投射破坏游戏 object - how do i get the raycast to destroy the game object 如何销毁实例化的游戏对象克隆? - How do I destroy an instantiated game object clone?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM