Search Unity

How do I de/activate a gameobject of the hierarchy ingame?

Discussion in 'Scripting' started by xSiraxXx, May 26, 2015.

  1. xSiraxXx

    xSiraxXx

    Joined:
    May 15, 2015
    Posts:
    24
    Hello,
    As I couldn't find any working scripts on how to pick up objects, I want to know how I can activate an object which is already on my hand but invisible and deactivate the object I'm colliding with (deactivate item on the ground and activate it on the hand)
     
  2. sluice

    sluice

    Joined:
    Jan 31, 2014
    Posts:
    416
    Your first problem should of been easily fixed..?
    Is your OnTriggerEnter working correctly?

    But to answer your new question...
    You can activate/deactivate a gamobject like so:
    Code (csharp):
    1. GameObject.SetActive
    For example:
    Code (csharp):
    1. gameObject.SetActive(false);
    therefore you just need a reference of the gameObject you want to active/deactivate.

    NOTE: you still need your OnTriggerEnter to work correctly to be able to deactivate that object.
    I would also add a tag, to be sure it's a pickable item or not.

    Code (csharp):
    1. void OnTriggerEnter(Collider collider)
    2. {
    3.     if(collider.tag == "pickable")
    4.     {
    5.         print(this is working!);
    6.         collider.gameObject.SetActive(false);
    7.     }
    8. }
    Log in the condition to be sure your Trigger is actually working.
     
    Last edited: May 26, 2015
    xSiraxXx likes this.
  3. xSiraxXx

    xSiraxXx

    Joined:
    May 15, 2015
    Posts:
    24
    Thanks it works!
    But I'll post something later maybe because of the other gameObject which should appear
     
  4. xSiraxXx

    xSiraxXx

    Joined:
    May 15, 2015
    Posts:
    24
    Everything works except that the gameobject I want to carry around doesn't follow me.
    Can you help me?
     
  5. sluice

    sluice

    Joined:
    Jan 31, 2014
    Posts:
    416
    @xSiraxXx, what are you doing for the object to follow you?
    You could always change the parent of the picked object so it's now a children of your player.
    Code (csharp):
    1. void OnTriggerEnter(Collider collider)
    2. {
    3.     if(collider.tag == "pickable")
    4.     {
    5.         collider.gameObject.transform.parent = player.transform;
    6.     }
    7. }
     
    xSiraxXx likes this.
  6. xSiraxXx

    xSiraxXx

    Joined:
    May 15, 2015
    Posts:
    24
    Thanks it works ^^
     
  7. sluice

    sluice

    Joined:
    Jan 31, 2014
    Posts:
    416
    Glad it does :)