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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

How to activate a child of a parent object?

Discussion in 'Scripting' started by Ninamori, Jan 7, 2016.

  1. Ninamori

    Ninamori

    Joined:
    Oct 30, 2015
    Posts:
    33
    I have a parent object that is not meant to be activated until later in the game. So I want to only activate when it enters a trigger.

    Code:

    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. publicclassSetActiveOnTriggerEnter:MonoBehaviour{
    5.  
    6. //public string FindGameOBJ; <-To be implemented for future re-usability. Ignore it for now.
    7. //public string ComponentToActivate; <-To be implemented for future re-usability. Ignore it for now.
    8.  
    9. voidOnTriggerEnter(Collider coll)// Collision detection.{if(coll.tag =="Ball")//Checks the tag to see if it is the PARENT OBJECT.{if(GameObject.Find("BallHealZone")==null)//Looks for the CHILD OBJECT, and if it is null than move forward.{Debug.Log("Activating BallHealZone! :)");//Tells me if it found said object, and confirms it is indeed null (yes it works).
    10. gameObject.SetActive(true);//Activates the CHILD OF PARENT OBJECT.}}}
    11.  
    12. }
    (No idea why the code won't format properly, but here it is on pastebin formatted properly.)

    As you can see it checks if the tag is correct finds the GameObject (the child is one that is supposed to be activated), logs it, and is supposed to set it active. The log says the conditions are met, but it doesn't fire off the gameObject.SetActive(true); command.

    How do I activate a child of a parent object?
     
  2. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    Code (csharp):
    1.  
    2. gameObject.SetActive(true);
    3.  
    set the current gameobject (the one the script is attached to) to active

    Code (csharp):
    1.  
    2. gameObject.transform.GetChild(0).gameObject.SetActive(true);
    3.  
    set the gameobject attached to the first child under the current gameobject's transform to active... o_O

    http://docs.unity3d.com/ScriptReference/Transform.html

    transforms control parent/child, gameobjects control active/inactive
     
  3. Ninamori

    Ninamori

    Joined:
    Oct 30, 2015
    Posts:
    33
    It didn't really work with the gameObject.SetActive(true); line, but gameObject.transform.GetChild(0).gameObject.SetActive(true); worked... sort of. It deactivates the trigger.
     
  4. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    I was explaining the code you are currently using, and showing you the code you need to use... a little "spot the difference" :p

    not sure what you mean by that, the trigger on what?
     
  5. Ninamori

    Ninamori

    Joined:
    Oct 30, 2015
    Posts:
    33
    The trigger that starts the entire process.

    It's a shame that SetActiveRecursively is obsolete. It'd be perfect for this.

    Anyway I looked at your link, and tried the follow...

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class SetActiveOnTriggerEnter : MonoBehaviour
    6. {
    7.  
    8.     //public string FindGameOBJ;
    9.     //public string ComponentToActivate;
    10.  
    11.     void OnTriggerEnter(Collider coll)
    12.     {
    13.         if (coll.tag == "Ball")
    14.         {
    15.             if (GameObject.Find("BallHealZone") == null)
    16.             {
    17.                 Debug.Log("Activating BallHealZone! :)");
    18.                 //gameObject.transform.GetChild(0).gameObject.SetActive(true);
    19.  
    20.                 foreach (Transform child in transform)
    21.                 {
    22.                     //child.gameObject.SetActive(true); <- Tried this first, but no dice.
    23.                     gameObject.transform.GetChild(0).gameObject.SetActive(true); //<-This doesn't work either.
    24.                 }
    25.             }
    26.         }
    27.     }
    28.  
    29. }
    Why is it such a PITA to access a game objects child?

    EDIT: gameObject.transform.GetChild(0).gameObject.SetActive(true); Looks like it should work. It accesses the transform, grabs the child, and should set it to active, right?
     
  6. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148

    Code (csharp):
    1.  
    2.     void OnTriggerEnter(Collider other)
    3.     {
    4.         if(other.tag == "Ball")
    5.         {
    6.             if (GameObject.Find("BallHealZone") == null)
    7.             {
    8.                 transform.GetChild(0).transform.gameObject.SetActive(true);
    9.             }
    10.         }
    11.     }
    12.  
    assuming BallHealZone is the child of the gameobject this script is attached to... which is the impression you've given in the code you've been posting at least
     
  7. Ninamori

    Ninamori

    Joined:
    Oct 30, 2015
    Posts:
    33
    Ok look. The script is attached to a trigger that just sits, and waits. When the object with the tag comes in it activates. It should look for the child named BallHealZone, and activate it. The trigger works fine, but it's a matter of activating the child.
     
  8. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148

    test script contains the above posted code... works fine.


    perhaps a more generalised approach would be easier?

    Code (csharp):
    1.  
    2. public GameObject target;
    3. public string triggerTag;
    4.  
    5. void OnTriggerEnter(Collider other)
    6. {
    7.     if(other.tag == triggerTag)
    8.     {
    9.         if (!target.activeSelf)
    10.         {
    11.             target.SetActive(true);
    12.         }
    13.     }
    14. }
    15.  
    that code removes the child/parent requirement so you could link together any trigger to activate any gameobject.
     
    Ninamori likes this.
  9. Ninamori

    Ninamori

    Joined:
    Oct 30, 2015
    Posts:
    33
    This works much better, and much cleaner. Thanks.
     
  10. tsondhi2006

    tsondhi2006

    Joined:
    Sep 4, 2016
    Posts:
    10
    this works, thanks man
     
  11. Ichihara13

    Ichihara13

    Joined:
    Sep 10, 2018
    Posts:
    1
    May I ask how can I add a line whish should hide any other object which doesn't match the tag?
     
  12. GreatWew_Lad

    GreatWew_Lad

    Joined:
    Sep 30, 2019
    Posts:
    1
    what i did is create a private gameobject with serialize field
    so i can link it in the editor

    and in code i just activate the gameobject i created(which is linked with the child in the editor)

    Code (CSharp):
    1. [SerializeField]
    2.     private GameObject _YourChild;
    3.  
    4.  
    5. public void ChildCanActivateNow()
    6.     {
    7.         _YourChild.SetActive(true);
    8.     }
    and i call the method when i want to activate it. Bit of a monkey code but it works..
     
  13. DonPuno

    DonPuno

    Joined:
    Dec 22, 2020
    Posts:
    57
    Thanks for sharing! This part works fine for me! :)