Search Unity

Activating and deactivating children of an object.

Discussion in 'Scripting' started by FormerKnownAs, Oct 12, 2010.

  1. FormerKnownAs

    FormerKnownAs

    Joined:
    Oct 12, 2010
    Posts:
    27
    Hi, i have started a platforming project and so far it's been going fairly well. But now i am stuck in a problem, which is that i need to be able to activate and deactivate a child of the player (a shield), but i cant seem to get it to work.
    I have tried some solutions but i cant get it to work.

    Do anyone have an idea how to get it to work?

    Thanks.
     
  2. Jesse Anders

    Jesse Anders

    Joined:
    Apr 5, 2008
    Posts:
    2,857
    Can you post the code that's not working, along with any error messages you're getting and/or a description of how it's not working?
     
  3. FormerKnownAs

    FormerKnownAs

    Joined:
    Oct 12, 2010
    Posts:
    27
    But thats the thing, i dont know to write to begin with. I know i want that if you press the down arrow the shield should activate and when you let go it should deactivate.

    But i dont really know how to code it, (very new to programming and having some trouble actually figuring out how to code what i want).
     
  4. Jesse Anders

    Jesse Anders

    Joined:
    Apr 5, 2008
    Posts:
    2,857
    You can find child objects of a game object using Transform.Find(); you can also find specific components in a game object using FindComponent(), FindComponentInChildren(), FindComponentsInChildren(), etc. (check the MonoBehaviour docs for more info). You can then activate/deactivate game objects or enable/disable components as needed.

    Does that help at all?
     
  5. FormerKnownAs

    FormerKnownAs

    Joined:
    Oct 12, 2010
    Posts:
    27
    Yes, i think it helped. So if i use Transform.Find(); what would i have to code to activate and deactivate an object? I searched around in the scripting referance but i cant seem to find anything.
     
  6. Jesse Anders

    Jesse Anders

    Joined:
    Apr 5, 2008
    Posts:
    2,857
    The GameObject class has an 'active' field, which you can set to true or false as needed.
     
  7. BooBi

    BooBi

    Joined:
    Jan 18, 2010
    Posts:
    534
    When you've find your children:

    public GameObject Model;



    Model.SetActiveRecursively(true/false);


    It will active/deactive the model which you want and all his children.
     
  8. FormerKnownAs

    FormerKnownAs

    Joined:
    Oct 12, 2010
    Posts:
    27
    I have tried some different solutions now but i still cant get it to fully work, i can get it to activate but i cant deactivate it. Here is my code, do you see anything wrong with it?

     
  9. Jesse Anders

    Jesse Anders

    Joined:
    Apr 5, 2008
    Posts:
    2,857
    Couple of things:

    1. Transform.Find() returns a reference to the transform associated with the object of that name, or null if no such object is found. In your code you're ignoring the return value, so those calls are basically no-ops.

    2. SetActiveRecursively() activates/deactivates the game object on which it's called, as well as all of its descendants. So, in your code you're actually activating/deactivating the game object to which the script is attached as well, which probably isn't what you want. If you only want to activate/deactivate the shield, you should work with the game object associated with the value returned by Transform.Find(), not the game object referenced by the 'gameObject' member field.
     
  10. CBif

    CBif

    Joined:
    Oct 3, 2010
    Posts:
    8
    So in short, try something like
    Code (csharp):
    1.  
    2. GameObject g = transform.find("Shield").gameObject;
    3. g.SetActiveRecursively(false);
    4.  
    Or, as you seem to already hold your shield transform in the variable "Shield", just call
    Code (csharp):
    1.  
    2. Shield.gameObject.SetActiveRecursively(false);
    3.  
     
  11. FormerKnownAs

    FormerKnownAs

    Joined:
    Oct 12, 2010
    Posts:
    27
    So on the second tip, you mean that i should put the script that finds and activate/deactivate the shield on a different object?
     
  12. Jesse Anders

    Jesse Anders

    Joined:
    Apr 5, 2008
    Posts:
    2,857
    No, you should keep it where it is, but modify the code so that it only activates/deactivates the object that you *want* to activate/deactivate. (See CBif's post for an example.)
     
  13. FormerKnownAs

    FormerKnownAs

    Joined:
    Oct 12, 2010
    Posts:
    27
    To be honest i got no idea what to code, for just starting out scripting this is a bit out of my knowledge, with all these words and rules etc...

    A question how did you learn programming?
     
  14. BooBi

    BooBi

    Joined:
    Jan 18, 2010
    Posts:
    534
    on your player add a

    public gameobjet Shield;

    then

    if(Input.GetKeyDown("down")){

    Shield.SetActiveRecursively(true/false);

    }


    In your player inspector drag'n'drop your shield into the Shield slot
    Then if your gameobject as children the recursively should work on them too. It will active/deactivate the gameobject and all his children, but not the parents of this gameobjet which, in this case, is your player (if I did understand).

    Why did you use the transform.find ?
     
    gogoroth0 likes this.
  15. FormerKnownAs

    FormerKnownAs

    Joined:
    Oct 12, 2010
    Posts:
    27
    I finally got it! Thanks so much for everyones help, and man it feels good to solve a scripting problem :D
     
  16. vished

    vished

    Joined:
    Oct 24, 2010
    Posts:
    26
    So gameObject.SetActiveRecursively(false) effectively deactivates the script itself? That's awesome!