Search Unity

Gameobject in children with tag

Discussion in 'Scripting' started by Darision, Feb 17, 2018.

  1. Darision

    Darision

    Joined:
    May 10, 2017
    Posts:
    4
    Hi, is there a simple way that i can do something along the lines of:

    Variable = GameObject.FindGameObjectsInChildrenWithTag("Tag").

    I currently have 2 game objects that fill an array of game objects with the given tag so both game objects take all of these objects in the editor rather than only those that are its children.
     
  2. fire7side

    fire7side

    Joined:
    Oct 15, 2012
    Posts:
    1,819
    I think you have to do:

    Code (csharp):
    1.  
    2. List<Transform> children = new List<Transform>();
    3. foreach(Transform child in transform){
    4. if(child.CompareTag("tag")){
    5. children.Add(child);
    6. }
    7. }
    8.  
    9.  
     
    Last edited: Feb 17, 2018
    CupSad likes this.
  3. Darision

    Darision

    Joined:
    May 10, 2017
    Posts:
    4
    That would work for storing the Transform, but how do I go about storing the Whole Gameobject
     
  4. Darision

    Darision

    Joined:
    May 10, 2017
    Posts:
    4
    Just found that you can retrieve the gameobject with the transform. cheers :D