Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by theUndeadEmo · Feb 07, 2012 at 11:54 AM · tagchildren

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
     }
   }
 }

 

   

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0

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); } } } }

Comment
Add comment · Show 6 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image theUndeadEmo · Feb 07, 2012 at 02:29 PM 0
Share

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

avatar image Aram-Azhari · Feb 07, 2012 at 02:42 PM 0
Share

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?

avatar image theUndeadEmo · Feb 07, 2012 at 02:54 PM 0
Share

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

avatar image Aram-Azhari · Feb 07, 2012 at 03:01 PM 0
Share

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.

avatar image theUndeadEmo · Feb 07, 2012 at 03:07 PM 0
Share

yes they are children

Show more comments
avatar image
0

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; }

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Set child object tags of instantiated GameObject 1 Answer

Check if there is a child with a tag? (multiple children but each diff tag) 1 Answer

Getting a tag from a child 1 Answer

destroying children also destroys parent itself 4 Answers

How can I get the tag of a child object? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges