Search Unity

How to disable a gameobject completely?

Discussion in 'Scripting' started by fulvioKidloom, Jul 19, 2017.

  1. fulvioKidloom

    fulvioKidloom

    Joined:
    Feb 20, 2017
    Posts:
    54
    Hi, I have an array of gameobjects and I want to use these one at a time.
    But, I noticed that the gameobjects, I disable using SetActive(false), components are still alive. The scripts are running for example.
    I would like to know how to disable completely without removing from the scene because I don't like to instantiate every time I have to use them.

    Thanks
     
  2. LaneFox

    LaneFox

    Joined:
    Jun 29, 2011
    Posts:
    7,532
    You can still invoke methods on scripts when they or their host object is 'inactive' but the MonoBehavior calls are not made.

    If you have this happening, maybe do a check on your methods to see if (gameObject.activeSelf) before running the function.
     
    fulvioKidloom and Kurt-Dekker like this.
  3. fulvioKidloom

    fulvioKidloom

    Joined:
    Feb 20, 2017
    Posts:
    54
    what happen with the box collider, sprite renderer and animator (components I have in the prefab) ? They are stilll active but are they processing something?
    Thanks
     
  4. LaneFox

    LaneFox

    Joined:
    Jun 29, 2011
    Posts:
    7,532
    No. They're not doing anything if the host gameObject is inactive.
     
    fulvioKidloom likes this.
  5. Flynn_Prime

    Flynn_Prime

    Joined:
    Apr 26, 2017
    Posts:
    387
    Awake is still called on scripts even when gameobjects are inactive