Null check not work for GameObject sometimes
来源:互联网 发布:linux rpm安装目录 编辑:程序博客网 时间:2024/06/02 06:53
Example Code:
void Test(object obj)
{
if (obj == null) {
return;
}
}
When the obj is an instance of GameObject, the null check will failed.
Reason
C# operators are overloaded - not overridden. This means that their implementation is determined at compile time - not runtime, and so is decided by the type of the reference - not the object being referenced.
In your case you are doing a null check on a variable of type object, which will invoke the == operator as defined for System.Object, the functionality of which is to check if the actual reference is equal to null, which it will not be for a reference to a destroyed GameObject.
To achieve the functionality you want, you need to also perform a null check with a reference of type GameObject, thus invoking the == operator as defined for GameObject, the functionality of which is to also equal null if the GameObject has been destroyed.
One example could be:
- if( obj ==null || (obj is GameObject && (GameObject)obj == null ) )continue;
Or (Because Equals method is overriden) :
obj ==null||obj.Equals(null)
https://forum.unity3d.com/threads/there-is-a-gameobject-is-in-a-stack-how-to-detect-if-it-has-been-destroyed.253196/
- Null check not work for GameObject sometimes
- Comparison of Layout_gravity and gravity. And sometimes Layout_gravity does not work
- check for String being null or empty?
- FindBugs:Compiler output path for module can not be null. check your module/project settings问题原因
- FindBugs:Compiler output path for module can not be null. check your module/project settings问题原因
- 数据库 设置字段check 如:not null 等
- SQLite 约束NOT NULL,DEFAULT,UNIQUE,PRIMARY KEY, CHECK
- NOT NULL constraint for VARCHAR colume
- the DataFormatString not work for the GridView BoundField problem
- when more is not good for your work
- Ubuntu NetWork Service Command for Network Restart Does Not Work
- Null or zero primary key encountered in unit of work clone exception not working 解决方法
- Excel import gives NULL rows with DTS Sometimes
- day19 Oracle ------ DDL(CREATE ,ALTER, constraints, PK, FK,UK,NOT NULL,CHECK) rename , truncate
- oracle的完整性约束:check/not null/unique/primary/foreign key
- SQLite学习笔记六: 约束NOT NULL,DEFAULT,UNIQUE,PRIMARY KEY, CHECK
- oracle约束总结(not null/unique/primary key/foreign key/check)
- oracle中五中约束详解(CHECK,UNIQUE,PRIMARY KEY,FOREIGN KEY,NOT NULL)
- Visual Studio 2013 Visual Studio 2015 百度网盘下载
- datatables中参数的处理(处理时间戳)
- 接口回调
- 项目管理一般知识 典型试题分析
- scrapy下载图片问题
- Null check not work for GameObject sometimes
- Powerdesigner 16.5 使用问题
- servlet小结
- UC/OS-II 的中断
- 什么是对象,为什么要面向对象,怎么才能面向对象?
- 跨域与跨域访问
- springMVC--处理器映射器
- shared_ptr:线程安全、循环引用
- 嵌入式软件:异步编程模型和传统编程模型