unity 使用interface 判断 null错误的问题

 

在使用Interface,并且由Monobehaviour继承Interface情况下,判断 interface的实际UnityEngine.Object是否null,出现错误,没有成功的判断出已经Destroy

 

https://gamedev.stackexchange.com/questions/128971/unity-c-interface-object-never-equals-null

解决方案:

https://discussions.unity.com/t/destroyed-object-not-comparing-to-null/88287

MonoBehaviour == operator is not overriden - it’s Object == operator. But I see that Equals method is overriden 

shootTarget == null || shootTarget.Equals(null)

原因:

shootTarget.Equals(null)实际调用的是UnityEngine.Object内复写的Equals,可以检测到UnityObject是否被Destroy

 

 

 

posted @ 2023-10-23 12:07  sun_dust_shadow  阅读(46)  评论(0编辑  收藏  举报