Reputation: 467
I have an object for which I can print its properties (like 'name', 'id',...) in a debug console. On the very next line, I print the object itself, and there I get 'null'. Kind of like this:
Debug.Log($"Item name is {item.name}");
Debug.Log($"Item is {item}");
if (item == null)
{
Debug.Log("Yes, item is null!");
}
So the first line will print a correct name, the second prints 'null'. The third line prints "Yes, item is null!".
How is that possible? (I don't know if this is relevant, but this happens in a code for Unity.)
Thank you, Arnaud.
Upvotes: 0
Views: 125
Reputation: 27011
That's because UnityEngine.Object overload ==
, !=
and bool
operators.
https://docs.unity3d.com/ScriptReference/Object-operator_eq.html
A Unity object will equal to null after it is destroyed, but you can still access part of its properties.
You can use the following condition to check whether a Unity object is completely null or not.
(object)item == null
Upvotes: 5