简体   繁体   English

如何修复 Unity 中的“'GameObject' 类型的对象已被销毁,但您仍在尝试访问它”错误?

[英]How to fix "the object of type 'GameObject' has been destroyed, but you are still trying to access it" error in Unity?

I'm creating a 2.5D fighting game in Unity with C#.我正在使用 C# 在 Unity 中创建一个 2.5D 格斗游戏。 Currently, I'm trying to make a bumper appear around the player and disappear after a set amount of time.目前,我正在尝试让一个保险杠出现在玩家周围并在一段时间后消失。 I've managed to make the bumper appear and disappear once, but after that, when I try to make the bumper appear again, Unity has an error for me: "The object of type 'GameObject' has been destroyed but you are still trying to access it."我已经设法让保险杠出现和消失一次,但在那之后,当我尝试再次出现保险杠时,Unity 对我有一个错误:“'GameObject' 类型的对象已被销毁,但您仍在尝试访问它。”

I've tried using the "instantiate" and "destroy" commands, following a tutorial by "Brackeys" on 2D shooting.我尝试使用“实例化”和“销毁”命令,遵循“Brackeys”关于 2D 拍摄的教程。 After also following some questions on forums about the same issue, I've altered my code again, but the problem persists.在论坛上也关注了一些关于同一问题的问题后,我再次修改了我的代码,但问题仍然存在。

The firePoint is an empty object, from where the BumperPrefab is instantiated. firePoint是一个空对象,从那里实例化 BumperPrefab。

using UnityEngine;

public class weapon: MonoBehaviour
{
    public Transform firePoint;
    public GameObject BumperPrefab;
    public float lifetime = 0.2f;

void Update()
{
    if (Input.GetButtonDown("Fire1"))
    {
        attack();
    }

}
void attack()
{
    BumperPrefab = (GameObject) Instantiate(BumperPrefab, firePoint.position, firePoint.rotation);
    Destroy(BumperPrefab, lifetime);
}

} }

I expect the GameObject "BumperPrefab" to appear, stick around for 0.2 seconds and disappear.我希望游戏对象“BumperPrefab”出现,停留 0.2 秒然后消失。 I should be able to repeat that as many times as I want, but what actually happens is that I can do this only once and then the error "The object of type 'GameObject' has been destroyed but you are still trying to access it" shows up and I can't make the BumperPrefab appear again.我应该能够根据需要重复多次,但实际发生的是我只能这样做一次,然后出现错误“'GameObject'类型的对象已被销毁,但您仍在尝试访问它”出现了,我不能让 BumperPrefab 再次出现。

Any help is much appreciated!任何帮助深表感谢!

using UnityEngine;

public class weapon: MonoBehaviour
{
    public Transform firePoint;
    public GameObject BumperPrefab;
    public float lifetime = 0.2f;

void Update()
{
    if (Input.GetButtonDown("Fire1"))
    {
        attack();
    }

}
void attack()
{
    var bumper = (GameObject) Instantiate(BumperPrefab, firePoint.position, firePoint.rotation);
    Destroy(bumper, lifetime);
}

Right now you're overwriting your public field containing the prefab object with your instantiated object, then destroying it.现在,您正在使用实例化对象覆盖包含预制对象的公共字段,然后销毁它。 Set the instantiated object as a variable and you should be fine.将实例化对象设置为变量,您应该没问题。

problem is you are destroying BumperPrefab问题是你正在破坏BumperPrefab

when you Instantiate a new GameObject you should add it to the local variable like this当你Instantiate一个新的GameObject你应该像这样将它添加到局部变量中

var newbumper = (GameObject) Instantiate(BumperPrefab, firePoint.position,firePoint.rotation);

and you must destroy your local variable which contains newly created gameObject并且您必须销毁包含新创建的gameObject局部变量

Destroy(newbumper , lifetime);

The problem is that in your code you don't care about is your GameObject exist.问题是在你的代码中你不关心你的 GameObject 是否存在。 So for example if (for some reason) the object BumperPrefab will not be created, Destory() will try to act on null.例如,如果(出于某种原因)对象 BumperPrefab 不会被创建,Destory() 将尝试对 null 进行操作。 You can try add to BumperPrefab script bumper.cs with:您可以尝试添加到 BumperPrefab 脚本bumper.cs:

float lifetime = 0.2f;

private void OnEnable()
{
Desroy(this, lifetime)
}

暂无
暂无

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

相关问题 如何修复“'GameObject' 类型的 object 已被破坏,但您仍在尝试访问它。” 统一 - How I can fix the “The object of type 'GameObject' has been destroyed but you are still trying to access it.” Unity “ GameObject”类型的对象已被破坏,但您仍在尝试访问它 - The object of type 'GameObject' has been destroyed but you are still trying to access it unity 'GameObject' 类型的 object 已被破坏,但您仍在尝试访问它 - unity The object of type 'GameObject' has been destroyed but you are still trying to access it 错误 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 Unity“类型的对象已被破坏”错误 - Unity 'the object of type has been destroyed' error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM