版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/linxinfa/article/details/89329849
假设我有一个延迟调用的接口
// 延迟2秒
DelayCallMgr.Call(()=>
{
// TODO
},2);
现在我有一个MyTest 脚本
using UnityEngine;
public class MyTest : MonoBehaviour
{
void Awake()
{
Destroy(gameObject);
// 延迟2秒调用
DelayCallMgr.Call(()=>
{
if(null != gameObject)
Debug.Log("null != gameObject");
else
Debug.Log("null == gameObject");
},2);
}
}
把脚本挂到一个空物体上,运行
会报错:
MissingReferenceException: The object of type ‘MyTest’ has been destroyed but you are still trying to access it.
因为gameObject其实是一个get方法,如果对象不存在,去调用get方法肯定就报错了,所以必须把gameObject缓存起来判断才行:
using UnityEngine;
public class MyTest : MonoBehaviour
{
GameObject m_selfGo;
void Awake()
{
m_selfGo = gameObject;
Destroy(m_selfGo);
// 延迟2秒调用
DelayCallMgr.Call(()=>
{
if(null != m_selfGo)
Debug.Log("null != gameObject");
else
Debug.Log("null == gameObject");
},2);
}
}