destroy(gameobject) 当前帧不会为null

这篇博客探讨了Unity中游戏对象的生命周期,特别是`Destroy`函数的使用。在`StartCoroutine(Wait())`后,`go`对象在第一帧不为null,但下一帧变为null。作者指出,判断对象是否已被销毁可能需要额外的变量进行记录,因为直接检查`go==null`在某些帧可能会得到错误的结果。博客内容涉及Unity的协程和对象生存期管理,对于理解和优化游戏性能有帮助。
摘要由CSDN通过智能技术生成

public class Test: MonoBehaviour
{
    public GameObject go;
    // Start is called before the first frame update
    void Start()
    {
        StartCoroutine(Wait());
    }
    IEnumerator Wait()
    {
        Destroy(go);
        go.name = "Cube";
        Debug.Log("go is null=>" + go == null);
        Debug.Log("gameobject=>" + go.gameObject);
        Debug.Log("gameobject.name=>" + go.gameObject.name);
        Debug.Log("transform=>" + go.transform.name);
        Debug.Log("parent.childCount=>" + go.transform.parent.childCount);
        Debug.Log($"0 go is null=> {go == null}");
        for (int i =1; i < 5; i++)
        {         
            yield return 1;
            Debug.Log($"{i} go is null=> {go == null}");
        }
    }
}

打印如下:

False

gameobject=>Cube (UnityEngine.GameObject)

gameobject.name=>Cube

transform=>Cube

parent.childCount=>1

0 go is null=> False

1 go is null=> True

2 go is null=> True

3 go is null=> True

4 go is null=> True

可以看出当前帧gameobjet不为null,而且没用报错,说明gameobject属性能使用。下一帧时,go 才为null。那么如何在当前在判断为是否为销毁,感觉只有用个变量记录了。有哪位大大知道更好的可以留言给我。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值