Hi there,
I’ve noticed that if I have an object which inherits from UnityEngine.Object, and it gets destroyed and so becomes null, the following happens:
Debug.Log( myObject ); // Outputs "null"
if( myObject == null )
{
// This code doesn't run?
}
if( !myObject )
{
// But this code does
}
So testing for null explicitly doesn’t work, presumably this is because there is actually an object there, but it has to behave as if it is null.
Now, I don’t like using the if( !myObject ) test, I think it’s bad practice. Even so, for the particular code I’m writing now I actually can’t use that, as I’m working with System.Object instances which may or may not also be UnityEngine.Object instances.
So, is this a bug, or a feature?
Edit: This code will very quickly show the problem I’m talking about:
using UnityEngine;
public class NewBehaviourScript : MonoBehaviour
{
private System.Object go;
private System.Object t;
public void Start()
{
GameObject temp = new GameObject( "go" );
this.go = temp;
this.t = temp.transform;
Destroy( temp );
}
public void Update()
{
Debug.Log( this.go == null );
Debug.Log( this.t == null );
}
}