0
\$\begingroup\$

If you have a Game Object, and you want to check if it exists, is

if (gameObject) { }

the same as

if (gameObject != null) { }

and if not, what is actually being evaluated in the first statement?

\$\endgroup\$
1

1 Answer 1

2
\$\begingroup\$

Here's a slightly more explicit test:

public class IfTest : MonoBehaviour {
    public GameObject toAssign;

    void TestIfs() {
        if (toAssign) {
            Debug.Log("if (gameObject) passes.");
        } else {
            Debug.Log("if (gameObject) fails.");
        }

        if (toAssign != null) {
            Debug.Log("if (gameObject != null) passes.");
        } else {
            Debug.Log("if (gameObject != null) fails.");
        }

        if (System.Object.ReferenceEquals(toAssign, null)) {
            Debug.Log("if (ReferenceEquals null) passes.");
        } else {
            Debug.Log("if (ReferenceEquals null) fails.");
        }
    }

    IEnumerator Start() {
        Debug.Log("-------Before assigning:----------");
        TestIfs();

        Debug.Log("-------Assigning:-----------------");
        toAssign = new GameObject();
        TestIfs(); 

        Debug.Log("-------Destroying:----------------");
        Destroy(toAssign);
        TestIfs();             

        // Wait one frame for the destruction to take effect.
        yield return null;
        Debug.Log("-------1 frame later:-------------");
        TestIfs();        

        Debug.Log("-------Unassigning:---------------");
        toAssign = null;
        TestIfs();                    
    }
}

Here's the output you get:

-------Before assigning:----------
if (gameObject) fails.
if (gameObject != null) fails.
if (!ReferenceEquals null) passes.
-------Assigning:-----------------
if (gameObject) passes.
if (gameObject != null) passes.
if (!ReferenceEquals null) passes.
-------Destroying:----------------
if (gameObject) passes.
if (gameObject != null) passes.
if (!ReferenceEquals null) passes.
-------1 frame later:-------------
if (gameObject) fails.
if (gameObject != null) fails.
if (!ReferenceEquals null) passes.
-------Unassigning:---------------
if (gameObject) fails.
if (gameObject != null) fails.
if (!ReferenceEquals null) fails.

So, the takeaways from this experiment that you can do yourself:

  • if (gameObject) and if (gameObject != null) behave the same.

  • Both will evaluate as true in the frame that their object is destroyed

  • Both will evaluate as false starting the frame after their object is destroyed.

  • !Object.ReferenceEquals(gameObject, null) will still evaluate as true on a destroyed object, because it's only been marked as invalid, it's not literally a null value.

  • In the Editor, !Object.ReferenceEquals(gameObject, null) will also evaluate as true on an unassigned object reference. The Editor inserts a special non-null placeholder object there secretly, so that it can give you better error messages if you try to use a reference you haven't assigned yet. But these placeholders still compare as equal to null with the other two methods, and aren't used at all in the built executable.

  • All three behave the same for a value that is actually, literally a null - like one you assigned there by hand.

You can repeat this test for a component reference and get the same results.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .