How to deal with nullreference exception?

When making UI, complicated situation because dev need it turned on,

but when game start, it normally should be turned off.

So when it is setactive(false), nullreference occurs very frequently.

How to deal get reference with deactivated gameobject?

That sounds like you’re getting it with FindObjectOfType (which doesn’t find deactivated objects), or GetComponentsInChildren without passing the “include inactive parameter”.

We deal with that situation by having all of our UI in a prefab, and instantiating that prefab on startup. When we instantiate it, we deal with all the references that needs to be dealt with, and then deactivate the parts that we need to deactivate.

2 Likes

So just GetComponent<> does not have [include inactive] option?

GetComponent works on inactive objects. It makes sense, since you’re passing it the object it looks at.