MissingReferenceException:“UnityEngine.GameObject”类型的对象已被销毁,但您仍在尝试从 EventSystem Unity 访问它

问题描述 投票:0回答:1

在我对游戏对象使用 Destroy() 后,Unity UI 的内置事件系统不断尝试访问按钮。当我暂停时它会停止并且不会再次开始,但否则会继续每一帧。该对象只需右键单击 -> UI -> 按钮 - Text Mesh Pro。 以下是按钮执行的函数和制作按钮的函数(按顺序)

    public void makeProgram(int i, Button button)
    {
        GameObject obj = Instantiate(ProgramObject, programHolder);
        programs.Add(obj.GetComponent<Program>());
        obj.GetComponent<Program>().gameManager = this;
        obj.GetComponent<RectTransform>().localPosition = button.transform.localPosition;
        Destroy (button.gameObject);
    }
    public void makeButton(int i)
    {
        Button button = Instantiate(ProgramBuyButton, programHolder)
        .GetComponent<Button>();
        button.name = i+"";
        button.GetComponent<RectTransform>().localPosition = Tools.makeProgramPosition(i - 1);
        button.onClick.AddListener(() => makeProgram(i, button));
    }

错误引用了InputSystemUIInputModule:476,这是以下代码中的第二行。

            // now issue the enter call up to but not including the common root
            Transform oldPointerEnter = eventData.pointerEnter?.transform;
            eventData.pointerEnter = currentPointerTarget;

这样做的问题是它会阻止用户与任何其他 UI 对象交互。 错误是:

Your script should either check if it is null or you should not destroy the object.
UnityEngine.Object+MarshalledUnityObject.TryThrowEditorNullExceptionObject (UnityEngine.Object unityObj, System.String parameterName) (at <adbae017f0374fce9921b97a33a4e8ca>:0)
UnityEngine.Bindings.ThrowHelper.ThrowNullReferenceException (System.Object obj) (at <adbae017f0374fce9921b97a33a4e8ca>:0)
UnityEngine.GameObject.get_transform () (at <adbae017f0374fce9921b97a33a4e8ca>:0)
UnityEngine.InputSystem.UI.InputSystemUIInputModule.ProcessPointerMovement (UnityEngine.InputSystem.UI.ExtendedPointerEventData eventData, UnityEngine.GameObject currentPointerTarget) (at ./Library/PackageCache/com.unity.inputsystem/InputSystem/Plugins/UI/InputSystemUIInputModule.cs:476)
UnityEngine.InputSystem.UI.InputSystemUIInputModule.ProcessPointerMovement (UnityEngine.InputSystem.UI.PointerModel& pointer, UnityEngine.InputSystem.UI.ExtendedPointerEventData eventData) (at ./Library/PackageCache/com.unity.inputsystem/InputSystem/Plugins/UI/InputSystemUIInputModule.cs:402)
UnityEngine.InputSystem.UI.InputSystemUIInputModule.ProcessPointer (UnityEngine.InputSystem.UI.PointerModel& state) (at ./Library/PackageCache/com.unity.inputsystem/InputSystem/Plugins/UI/InputSystemUIInputModule.cs:352)
UnityEngine.InputSystem.UI.InputSystemUIInputModule.Process () (at ./Library/PackageCache/com.unity.inputsystem/InputSystem/Plugins/UI/InputSystemUIInputModule.cs:2257)
UnityEngine.EventSystems.EventSystem.Update () (at ./Library/PackageCache/com.unity.ugui/Runtime/UGUI/EventSystem/EventSystem.cs:530)

我试图找到一种“安全”销毁按钮的方法,但我找不到任何与事件系统相关的东西,我想将其从光标移开,并使用协程在一段时间后销毁它(编辑:我测试了一下,确实如此,但仍然想要一个更好的解决方案)工作,但也不是最优雅的,我希望找到一种更好的方法来处理这个问题。感谢您的阅读,我希望对此有更好的解决方案。

c# unity-game-engine unity-ui
1个回答
0
投票

听起来像是一个奇怪的 EventSystem 边缘情况,你破坏了当前事件正在触发的东西。

没有常规的解决方法可能是使用

Destroy
的延迟并执行例如

button.gameObject.SetActive(false); 
Destroy(button.gameObject, 1);

这只会停用按钮并在 1 秒后销毁它。

一秒钟可能有点过分,但只是为了确定

© www.soinside.com 2019 - 2024. All rights reserved.