How to instantiate a prefab and hide it

I want to instantiate an addressable but I want to configure it before it displays on screen. Is there a way to instantiate it without it rendering on screen, and then I can decide when to render it?

1 Like

It is a config of prefab. Uncheck the box above “Tag” on the inspector as the attached file.

6606937--751681--uncheck.jpg

Or just wait for the instantiate operation and call SetActive(false) on the GameObject instance.

If I do that I can’t access scripts on the object and that also causes a hiccup. It is actually recommended to disable components and not the GameObject.

The problem with disabling the gameObject, I can’t access the components until reenabling the GameObject.

Yes, disable component is another choice.

If gameObject.SetActive(false), the default events of MonoBehaviour will not be called, e.g Start(), OnEnable(). But you still can access this gameObject’s scripts.

By whom, and under what circumstances? I don’t think that would be a valid recommendation in many cases. Maybe if what you have is a UI object with a canvas, usually disabling/enabling the canvas is better than deactivating/activating the object, but it also depends on several factors, for example whether other components do any updates periodically.

According to Unity, disabling and enabling a GameObject is an expensive operation which causes hiccups. When you also disable a GameObject, you can’t access the components. When you disable a component while The GameObject is still active, you are still able to access the disabled script (does not return null) if referencing it. It is a best practice. Also when You disable a GameObject, it causes things like animators to reset.

Do you have at hand the source to Unity’s recommendations, to see what situations they were referring to? As I mentioned, it depends on the situation, sometimes you’ll need to disable one or several components, while other times it’ll be more appropriate to just deactivate the GameObject and it won’t necessarily cause hiccups, so I wouldn’t try to go out of my way to never deactivate them.

As Gearmoon indicated, you can definitely access a component on a deactivated GameObject, by normal means. Maybe you are referring to components in inactive child objects? GetComponentInChildren has 2 versions, one without parameters that doesn’t return components on inactive GameObjects, and another with the parameter includeInactive which you can set to true to get components from any object, active or inactive.

It was in one of the last pro subscriber only virtual meetups. Basically the main issue with deactivating a game object (in terms of performance) is it creates garbage for every component attached to the GameObject and every component on the each child GameObject. And depending on the code, there could be even more garbage created. That causes a hiccup because the garbage collector has to do extra work every time a GameObject is disabled. Whereas disabling only the component you want disabled only causes garbage for the single component, not for all the components atttached to the GameObject and its children; So the performance hit is smaller.