Why is uninitialized GameObject != null?

I have an script object with references:

public GameObject prefabObject;
private GameObject instanceObject;

Which is self-explanatory;

Then I want to show and hide it this way:

private void ShowObject()
    if (this.instanceObject = null)
        this.instanceObject = (GameObject)Instantiate(this.prefabObject);

private void HideObject()
    if (this.instanceObject != null)

But when trying to instantiate it, it never comes under “if null” check, like instanceObject is never null. Why is that?


It never goes through the ‘equals to null’ check because it seems as if you only have one equals sign instead of two. I’m not really sure if that’s what’s going on in your actual script, or if you just made an error copying it over, so I can’t know if I’m correct.

So this line: if (this.instanceObject = null) should be: if (this.instanceObject == null).

If you need any further assistance , please ask.

Best of luck,