【Unity】判断游戏物体是否存在

如果一个物体销毁后仍然访问它,就会报错如下

The object of type 'GameObject' has been destroyed but you are still trying to access it.

解决这个问题的方法是访问这个物体前判断它是否存在 ,那如何判断呢?很简单,直接把物体作为判断条件就可以了

如果物体存在则返回True,否则返回False

GameObject obj;

//if(obj == true)
if(obj)
{
    Debug.Log("obj is exists.");
}

posted on 2018-12-20 23:53  real-道森  阅读(929)  评论(0)    收藏  举报