- Home /
Finding children of a gameobject with a certain tag
I have walls in my scene, each wall has a set of waypoints in it with the tag waypoint
what i want to know is how to put the child way points into an array
code so far
private GameObject[] waypoints;//<---- need to get the children with tag"waypoint" in here
private GameObject[] walls;
void Start{
walls = GameObject.FindGameObjectsWithTag ("wall");
}
void Update(){
foreach(GameObject wall in walls) {
foreach (Transform Child in wall.transform) {
//stuck here, need to get all the children with the tag "waypoint" into the game object array called waypoints
}
}
}
Answer by Aram-Azhari · Feb 07, 2012 at 01:55 PM
Try this, modify it to suite your code:
List<GameObject> waypoints=new List<GameObject>(); private GameObject[] walls;
void Update(){ foreach(GameObject wall in walls) { Transform[] childrenOnthisWall = gameObject.GetComponentsInChildren<Transform>(); foreach (Transform child in childrenOnthisWall){ if (child.tag == "waypoint"){ waypoints.Add(child.gameObject); } } } }
i believe you have misread the question
there is not script attached to the wall... around the wall are waypoints (spheres)
i need to get the children in this wall, with the tag waypoint into an array
(the bit below i can do) then i am going to use this array and calculate the longest distance to the player and move the player
I think you have asked two questions, without providing enough information. The first one was the title "Finding children of an object with a certain tag" which is answered.
Also, you mentioned "each wall has a set of waypoints in it". Does this mean the Wall gameobject has waypoint gameobjects as its children?
If the code is not attached to the wall, where is it attached to? Spheres?
nope its still a single question
my level has 10 walls in it each wall has 4 waypoints inside it
This is my plan
get all walls into an array calc the shortest distance to the wall
once this is found, i will then get the waypoints inside this wall and add it to the array i will then calc the furthest distance then the enemy AI will move to this location
current problem is getting the children of this wall into an array so i can perform these calcs
your method does not work becuase i am adding the walls into the array and finding the shortest distance using a loop, this inturn keeps adding the waypoints to the array infinately
ok let me see if i understood correctly. What do you mean waypoints are inside wall ? Are you saying they are positioned inside the wall? and they are not the children of the wall in the Hierarchy tab of editor? If they are not children of the walls, then you can make the waypoints inside a wall, children of that wall. The code I wrote will not work if the waypoints.
I hope you find a way.
Answer by aldonaletto · Feb 07, 2012 at 11:58 PM
I think your logic is wrong: if you search for the farthest waypoint each Update, the AI may enter an infinite loop. When needed, you should use FarthestWaypoint(NearestWall()) to find the desired waypoint:
private GameObject[] walls;
void Start{ walls = GameObject.FindGameObjectsWithTag ("wall"); }
// to find the desired waypoint, use this: ... GameObject waypoint = FarthestWaypoint(NearestWall()); ...
// these are the functions:
GameObject NearestWall(){ float distance = Mathf.Infinity; GameObject nearest; foreach (GameObject wall in walls){ // get its squared distance to save a few CPU cycles... float dist2 = (wall.transform.position - transform.position).sqrMagnitude; if (dist2 < distance){ // if closer than current min distance... nearest = wall; // this is the new nearest wall distance = dist2; } } return nearest; }
GameObject FarthestWaypoint(GameObject wall){ float distance = -1; GameObject farthest; foreach (Transform child in wall.transform){ if (child.CompareTag("waypoint")){ // if found a waypoint... // get its squared distance to save a few CPU cycles... float dist2 = (child.position - transform.position).sqrMagnitude; if (dist2 > distance){ // if farther than current max distance... farthest = child.gameObject; // this is the new farthest object distance = dist2; } } } return farthest; }