after the object is destroy, the original reference to the object ==null the problemTags: unityc# inheriting objects2017-01-23 23:32 506 People read Comments (0) favorite reports Classification:Unity (+)
Copyright NOTICE: Welcome Reprint, note the source can ~
Because C # itself has a GC mechanism, when the object's reference is 0 garbage collection, the corresponding reference will be set to null, but unity inside, the tune destroy delete an object, just freed the unity of resources, and at the C # level, The reference to this object is still there, so it will not be garbage collected, so the C # layer's resources are not freed, but the reference to it is actually equal to NULL. The code is followed by the implementation of the Unity object script, and the Monobehaver in unity is inherited from object, including all component. After following the object class, I found these words:
[CSharp]View PlainCopyprint?
- public static bool operator == (object x, object y );
- public Static bool operator != (object x, object y);
The object class overloads the operators = = and! =, so that after a unity object is destroy, the resources in the C # layer are not actually freed, and when the corresponding reference variable is used to do = = = = = To determine, because the corresponding instance still exists, it will go to is overloaded with the = = and! = operator, and unity directly returns a true. It's basically clear to be here, but when we talk to colleagues today, Found to use System.Object to refer to a Unity Object object, and then destroy this object, and then take this System.Object reference to the null comparison, the return is true, was not figured out, because System.Object is C #自己的类, and does not overload = = and! = operator, which is expected to return false. Now think about it and forget about the object-oriented concept. System.Object is the parent class of all classes in C #, and Unity.object is written in C #, Natural System.Object is also the parent of Unity.object, then take a parent class to refer to the object to a subclass instance, and the subclass instance overloads the method of the parent class, then the method of the parent class is naturally hidden away, the actual adjustment is the sub-class overloaded method. So in the case of the above System.Object reference Unity.object, the object is destroy, since the instance of the C # layer is not released, so when using a System.Object reference with NULL = = The decision of the time is actually walking or unity.object in the overloaded this = =, because here the unity.object is actually the System.oobject subclass. Here is another operator in C # that is used to sentence null,?? This operator is not overloaded by Unity.object, and the object after destroy is not returned to true.
The role of destroy and Null in the recovery mechanism of C #-----------------------------