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。那么如何在当前在判断为是否为销毁,感觉只有用个变量记录了。有哪位大大知道更好的可以留言给我。