- Home /
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)
{
Destroy(this.instanceObject);
}
}
But when trying to instantiate it, it never comes under "if null" check, like instanceObject is never null. Why is that?
Usually equality is tested with == dunno if different rules apply for testing null
Answer by SeeSharp · Apr 23, 2014 at 04:06 AM
Hi,
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,
SeeSharp.
I'm dumb. :) Bet it will be an 3000 A.D. and people still will get caught in it.
Strange that VS2013 Express didn't warned me. Or result of an assignment "null" to an object returns boolean, I dunno...
Your answer
![](https://koobas.hobune.stream/wayback/20220613143350im_/https://answers.unity.com/themes/thub/images/avi.jpg)