简体   繁体   English

Unity 试图访问被破坏的 object

[英]Unity trying to access destroyed object

I instantiate a prefab object (bullet) and if it gets destroyed while the yield return seconds, Unity throws an error at the line where I set the rigidbody velocity to zero.我实例化了一个预制件 object(子弹),如果它在 yield return 秒时被销毁,Unity 会在我将刚体速度设置为零的那一行抛出错误。 It doesn't care about object null checks wherever I put them.它不关心 object null 支票无论我把它们放在哪里。 The script is attached to the Instantiated gameobject.该脚本附加到实例化的游戏对象。

public IEnumerator GetOutState (GameObject target) {

        state = State.GetOut;
        if (state == State.GetOut && gameObject != null)  {
            rbMissile.velocity = -transform.up * speed;

            yield return new WaitForSecondsRealtime(1);

            rbMissile.velocity = Vector2.zero;


            StartCoroutine(FlyState(target));

        }

    }

What game object is this script attached to?这个脚本附在什么游戏object上? You are checking if the gameobject attached to the script is null with您正在检查附加到脚本的游戏对象是否为 null

if (state == State.GetOut && gameObject != null)  {

did you mean to put你是不是想放

if (state == State.GetOut && rbMissile != null)  {

Also if this does not solve the issue, your issue is likely that:此外,如果这不能解决问题,您的问题可能是:

StartCoroutine(FlyState(target));

Something in the FlyState method is referencing the null game object FlyState 方法中的某些内容引用了 null 游戏 object

if如果

if (state == State.GetOut && rbMissile != null)  { 

didn't fix it your missle is getting destroyed between when this method is called and the one second delay from:没有修复它你的导弹在调用此方法和一秒延迟之间被摧毁:

yield return new WaitForSecondsRealtime(1);

Two possible fixes: change the IEnumerator to a void method and remove两个可能的修复:将 IEnumerator 更改为无效方法并删除

 yield return new WaitForSecondsRealtime(1);

or else add in another check after a second passes或者在一秒钟后添加另一张支票

if (rbMissle != null) {
rbMissile.velocity = Vector2.zero;
}

暂无
暂无

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

相关问题 unity 'GameObject' 类型的 object 已被破坏,但您仍在尝试访问它 - unity 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? 文本类型的 object 已被破坏,但您仍在尝试访问它 - Unity - The object of type Text has been destroyed but you are still trying to access it - 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 在Unity中重新生成破坏的对象 - Respawn destroyed object in Unity “ GameObject”类型的对象已被破坏,但您仍在尝试访问它 - The object of type 'GameObject' has been destroyed but you are still trying to access it “CanvasGroup”类型的对象已被销毁,但您仍在尝试访问它 - The object of type "CanvasGroup" has been destroyed but you are still trying to access it “BallMovement”类型的 object 已被销毁,但您仍在尝试访问它 - The object of type 'BallMovement' has been destroyed but you are still trying to access it Unity for 循环尝试访问已销毁的游戏对象 - Unity for loop try to access a destroyed GameObject 在被破坏对象中的Unity C#StartCoroutine() - Unity C# StartCoroutine() in Destroyed Object
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM