Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Access the Child of a game object

Discussion in 'Scripting' started by chrisswingsx, Jan 7, 2020.

  1. chrisswingsx

    chrisswingsx

    Joined:
    Oct 28, 2018
    Posts:
    19
    I am trying to instantiate characters with code that works well for me, if I instantiate that prefabricated character, but when the character is on the scene it has 3 parameters and when I return it to a prefabricated one only 2 are passed and that is giving me problems. I would like to know if there is any way to find that game object, I was dealing with GameObject.GetChild.FindGameObjectWithTag (""); and he doesn't find it or it will be because the object is a child of another game object. I need to find the Pipe System object when it is instantiated, Sorry my English is not so good. I hope it is understood with the images

    https://ibb.co/t46wfL5
    https://ibb.co/Gpp3QJh
    https://ibb.co/yRw2ph1
     
  2. Chocolade

    Chocolade

    Joined:
    Jun 19, 2013
    Posts:
    932
    If you want to find it by tag :

    Code (csharp):
    1.  
    2. GameObject.FindGameObjectWithTag("Untagged");
    3.  
    I don't see in the screenshot that you gave any tag to the Player White
    So doing it this way is wrong since it will find you any gameobject that is untagged and by default gameobjects are untagged.

    So add a tag to Player White for example Player White then change the Tag from Untagged to Player White
    Then you can do :

    Code (csharp):
    1.  
    2. GameObject.FindGameObjectWithTag("Player White");
    3.  
    When the gameobject have a tag and you find it by tag it will find it even if it's a child.

    Now to get access to the parameters in the script Player just do :

    Code (csharp):
    1.  
    2. GameObject.FindGameObjectWithTag("Untagged").GetComponent<Player>().PipeSystem;
    3.  
    Or

    Code (csharp):
    1.  
    2. GameObject.FindGameObjectWithTag("Untagged").GetComponent<Player>().Velocity;
    3.  
    and then you can do for example :

    Code (csharp):
    1.  
    2. GameObject.FindGameObjectWithTag("Untagged").GetComponent<Player>().Velocity = 20f;
    3.  
     
    chrisswingsx likes this.