Search Unity

Activating and deactivating a gameobject

Discussion in 'Scripting' started by Soumikbhat, Oct 23, 2014.

  1. Soumikbhat

    Soumikbhat

    Joined:
    Nov 23, 2013
    Posts:
    110
    Hello, I want to turn 'on' and 'off' a gameobject based on the 'spacebar' input...
    So I'd want my Update() to be something like :

    Code (CSharp):
    1.  
    2. void Start () {
    3.         this.gameObject.SetActive(false);
    4.     }
    5. void Update () {
    6.        
    7.         if (Input.GetKeyDown("space"))
    8.            
    9.             this.gameObject.SetActive(true);
    10.         else this.gameObject.SetActive(false);
    11.        
    12.     }
    But according to this Unity Doc once a ameobject is set to 'False' it turns off any attached renderers, colliders, rigidbodies, scripts, etc, so this won't work obviously.

    There's another way in which this may work and that is, by making this a child of an empty gameobject, and then applying the script to this empty gameobject turning off and on its child-transforms. However, for some reason, the position of a gameobject changes on being made a child - I don't really want that.

    Any other way of making this object visible and invisible based on the 'spacebar' input?
     
  2. fffMalzbier

    fffMalzbier

    Joined:
    Jun 14, 2011
    Posts:
    3,276
    The question is for me, what do you what to tun off or on? Another objects scripts or just the render ?
    If you what to disable another component , use GetComponent and disable the component as you need.
    If you like to keep the script running but do not like to keep all the code in the Update function running , just some of them , just set and check a bool variable to see if the script should run all the code.

    For re-parenting and that causing a preposition: The values in the inspector are coordinates relative to the parent, the global position does not change only the position relative to the parent since the parent has changed.
     
  3. Soumikbhat

    Soumikbhat

    Joined:
    Nov 23, 2013
    Posts:
    110
    Lets assume that I have a gun..and I want it to stay activated only when I press the spacebar..deactivated( or invisible) otherwise. What do I do to achieve this?
     
  4. Kalaskrysset

    Kalaskrysset

    Joined:
    May 16, 2014
    Posts:
    9
    I have not tried it but something like this could do the trick:

    this script would be on the parent of the gun-gameobject.
    Code (CSharp):
    1. public GameObject gun;
    2.  
    3. void Start()
    4. {
    5. gun.SetActive(false);
    6. }
    7.  
    8. void Update()
    9. {
    10. if(Input.GetButton("ShowGun")) // you would create this in the input manager
    11. {
    12. gun.SetActive(true);
    13. }
    14. else
    15. {
    16. gun.SetActive(false);
    17. }
    18. }
     
  5. Soumikbhat

    Soumikbhat

    Joined:
    Nov 23, 2013
    Posts:
    110
    I know that, but what I'm asking for is that - can this be achieved without making the gun-gameobject a child?
     
  6. Kalaskrysset

    Kalaskrysset

    Joined:
    May 16, 2014
    Posts:
    9
    Ah sorry about that then.
    Try:
    Code (CSharp):
    1. renderer.enabled = false;
     
    Soumikbhat likes this.
  7. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    Or, have your spacebar-handling script live somewhere else, and simply have a "public GameObject gun" property, which you set to the gun object. Then that script can activate and deactivate the gun however you like.
     
    Soumikbhat likes this.
  8. Soumikbhat

    Soumikbhat

    Joined:
    Nov 23, 2013
    Posts:
    110
    Exactly..Thanks..