Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

How to instantiate a prefab and hide it

Discussion in 'Addressables' started by apiotuch_unity, Dec 8, 2020.

  1. apiotuch_unity

    apiotuch_unity

    Joined:
    Jun 28, 2019
    Posts:
    138
    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?
     
    Kazko likes this.
  2. Gearmoon

    Gearmoon

    Joined:
    Oct 14, 2015
    Posts:
    7
    It is a config of prefab. Uncheck the box above "Tag" on the inspector as the attached file.
     

    Attached Files:

  3. Trisibo

    Trisibo

    Joined:
    Nov 1, 2010
    Posts:
    233
    Or just wait for the instantiate operation and call SetActive(false) on the GameObject instance.
     
  4. apiotuch_unity

    apiotuch_unity

    Joined:
    Jun 28, 2019
    Posts:
    138
    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.
     
  5. apiotuch_unity

    apiotuch_unity

    Joined:
    Jun 28, 2019
    Posts:
    138
    The problem with disabling the gameObject, I can't access the components until reenabling the GameObject.
     
  6. Gearmoon

    Gearmoon

    Joined:
    Oct 14, 2015
    Posts:
    7
    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.
     
  7. Trisibo

    Trisibo

    Joined:
    Nov 1, 2010
    Posts:
    233
    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.
     
  8. apiotuch_unity

    apiotuch_unity

    Joined:
    Jun 28, 2019
    Posts:
    138
    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.
     
  9. Trisibo

    Trisibo

    Joined:
    Nov 1, 2010
    Posts:
    233
    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.
     
  10. apiotuch_unity

    apiotuch_unity

    Joined:
    Jun 28, 2019
    Posts:
    138
    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.