简体   繁体   English

unity 'GameObject' 类型的 object 已被破坏,但您仍在尝试访问它

[英]unity The object of type 'GameObject' has been destroyed but you are still trying to access it

I have a problem that when i change scenes from the menu to the game and when i click i will get the error 'The object of type 'GameObject' has been destroyed but you are still trying to access it.'我有一个问题,当我将场景从菜单更改为游戏并单击时,我会收到错误消息“'GameObject' 类型的 object 已被破坏,但您仍在尝试访问它。” and this is happening while the menucontroller is not in the game scene, i have been working on this for the past few days to fix it but cant find how to do it.当菜单控制器不在游戏场景中时,就会发生这种情况,过去几天我一直在努力修复它,但找不到解决方法。 If anyone can help that would be great this kind of question has been asked alot but I cant find a solution for my problem.如果有人可以提供帮助,那就太好了,这种问题已经被问了很多,但我找不到解决我的问题的方法。

We also have a similar script that has the same code except instead of sceneswitching its for interaction so when we try to interact with something we get the error我们也有一个类似的脚本,它具有相同的代码,除了场景切换它的交互之外,所以当我们尝试与某些东西交互时,我们会得到错误

Thank you for your help.谢谢您的帮助。

menucontroller菜单控制器

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;
using UnityEngine.SceneManagement;
using API;

public class MenuController : MonoBehaviour
{
    [SerializeField] private InputActionReference shoot;
    [SerializeField] private InputActionReference shoot2;
    public GameObject sendObject;
    public GameObject sendObject2;
    public float range = 1000;




    void Start()
    {
        shoot.action.performed += Shoot;

       
    }

    async void Shoot(InputAction.CallbackContext obj)
    {
        RaycastHit hit;

        if (Physics.Raycast(sendObject.transform.position, sendObject.transform.forward, out hit, range))
        {
            SceneManager.LoadScene("SampleScene");
        }
    }
}    

The error message sounds quite self-explanatory.错误消息听起来很不言自明。

You are trying to access an object that already was destroyed.您正在尝试访问已被破坏的 object。

Since you load a new scene由于您加载了新场景

SceneManager.LoadScene("SampleScene");

the current scene and its objects are unloaded / destroyed.当前场景及其对象被卸载/销毁。

Whenever you deal with events每当你处理事件

void Start()
{
    shoot.action.performed += Shoot;
}

make sure to also remove the callback as soon as not needed/valid anymore确保在不再需要/不再有效时也删除回调

private void OnDestroy()
{
    shoot.action.performed -= Shoot;
}

暂无
暂无

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

相关问题 “ GameObject”类型的对象已被破坏,但您仍在尝试访问它 - The object of type 'GameObject' has been destroyed but you are still trying to access it 如何修复 Unity 中的“'GameObject' 类型的对象已被销毁,但您仍在尝试访问它”错误? - How to fix "the object of type 'GameObject' has been destroyed, but you are still trying to access it" error in Unity? 如何修复“'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:“GameObject”类型的 object 已被破坏,但您仍在尝试访问它 - Error MissingReferenceException: The object of type 'GameObject' has been destroyed but you are still trying to access it 文本类型的 object 已被破坏,但您仍在尝试访问它 - Unity - The object of type Text has been destroyed but you are still trying to access it - Unity “CanvasGroup”类型的对象已被销毁,但您仍在尝试访问它 - The object of type "CanvasGroup" has been destroyed but you are still trying to access it Unity “GameObject”类型的 object 已被销毁 - Unity The object of type “GameObject” has been destroyed Brackeys 2d platformer GameObject'已被破坏,但您仍在尝试访问它 - Brackeys 2d platformer GameObject' has been destroyed but you are still trying to access it MissingReferenceException:类型为“攻击者”的对象已被破坏,但您仍在尝试访问它 - MissingReferenceException: The object of type 'Attacker' has been destroyed but you are still trying to access it “healthscript”类型的游戏 object 已被销毁,但您正在尝试访问它 - The game object of type “healthscript” has already been destroyed but you are trying to access it
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM