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
0
Question by valtteri_m · Apr 29, 2015 at 03:31 PM · javascriptparentchildtagfind

Find children by tag from Player

Ive been searching for an awnser for this for ever, what I want is to have a var Objects : GameObject[]; and the Objects would be found with ItemHolder tag and that they would be children of an object called Player

how? And I use javascript btw

Comment
Add comment · Show 5
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 screenname_taken · Apr 29, 2015 at 03:46 PM 0
Share

var vroomvroom:GameObject[]; <-- that's how it should be for JS. (Or more politically correct, Unity Script :P)

avatar image valtteri_m · Apr 29, 2015 at 03:48 PM 0
Share

uh whoops I just wrote it wrong there, but how do I find them??

avatar image Soos621 · Apr 29, 2015 at 03:55 PM 0
Share

You can use foreach(transform child in gameobject){if(child.tag.equals("tag")){\\maybe add them to a generic list or array ? } } This will search out all the transforms in your player game object I'm not positive on the js syntax but you should be able to convert that from c# easily enough

avatar image valtteri_m · Apr 29, 2015 at 04:14 PM 0
Share

I had a similar line of script before but I couldn't get it converted. I haven't used foreach before.. also when I was looking into this foreach thing I figured out I could just do Number++ for each child it finds from player with the tag.. uh help!!

avatar image tahir ul nasir · May 01, 2015 at 01:14 AM 0
Share

hi .what you want to do ..?? if u want to access the child game object get through only tag of the child .if u want to get through parent then use Find Child() command and then check the tags of child using Foreach loop

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by eleazar_dev · Apr 30, 2015 at 11:53 AM

Here's a project I wrote a while ago:

alt text

The cube is the "Player", the spheres are the item holders, and the cilinder is the GameObject that uses a script to get the children (I called it ChildFinder).

In case you need it, I wrote it both in C# and... Unity Script ;)

// JavaScript

 #pragma strict
 
 var _itemHoldersWithFor : ArrayList;
 var _itemHoldersWithForEach : ArrayList;
 var _player : GameObject;
 
 function Awake () {
     _player = GameObject.Find("Player");
     _itemHoldersWithFor = new ArrayList();
     _itemHoldersWithForEach = new ArrayList();
 }
 
 function PrintArrayList (name : String, array : ArrayList) {
     Debug.Log("ArrayList: "+name);
     for (item in array) {
         var gobj : GameObject = item as GameObject;
         Debug.Log("Child: "+gobj.name);
     }
     Debug.Log("***********");
 }
 
 function Start () {
     if (_player != null) {    
 
         var childrenTransforms : Transform[];
         childrenTransforms = _player.GetComponentsInChildren.<Transform>();
         
         // 1. 
 
         var childCount = childrenTransforms.Length;
         
         if (childrenTransforms != null) {
             for(var i = childCount - 1; i >= 0; i--) {
                 if (childrenTransforms[i].gameObject.tag == "ItemHolder")
                     _itemHoldersWithFor.Add(childrenTransforms[i].gameObject);
             }
         }
 
         PrintArrayList("ItemHolders with FOR", _itemHoldersWithFor);
         
         // 2.
         
         for (item in childrenTransforms) {
             var childTransform : Transform = item as Transform;
             if (childTransform != null && childTransform.gameObject.tag.Equals("ItemHolder"))
                 _itemHoldersWithForEach.Add(childTransform.gameObject);
         }
 
         PrintArrayList("ItemHolders with FOREACH", _itemHoldersWithForEach);
     }
 }
 
 function Update () {
 
 }

// C#

 using UnityEngine;
 using System.Collections;
 
 public class ChildFinder : MonoBehaviour {
 
     private ArrayList _itemHoldersWithFor;
     private ArrayList _itemHoldersWithForEach;
     private GameObject _player;
 
     void Awake () {
         _player = GameObject.Find("Player");
         _itemHoldersWithFor = new ArrayList();
         _itemHoldersWithForEach = new ArrayList();
     }
 
     void PrintArrayList (string name, ArrayList array) {
         Debug.Log("ArrayList: "+name);
         foreach (GameObject gobj in array) {
             Debug.Log("Child: "+gobj.name);
         }
         Debug.Log("***********");
     }
 
     // Use this for initialization
     void Start () {
         if (_player != null) {    
 
             Transform[] childrenTransforms = _player.GetComponentsInChildren<Transform>();
             
             // 1. 
 
             int childCount = childrenTransforms.Length;
             
             if (childrenTransforms != null) {
                 for(int i = childCount - 1; i >= 0; i--) {
                     if (childrenTransforms[i].gameObject.tag == "ItemHolder")
                         _itemHoldersWithFor.Add(childrenTransforms[i].gameObject);
                 }
             }
 
             PrintArrayList("ItemHolders with FOR", _itemHoldersWithFor);
             
             // 2.
             
             foreach (Transform childTransform in childrenTransforms) {
                 if (childTransform != null && childTransform.gameObject.tag.Equals("ItemHolder"))
                     _itemHoldersWithForEach.Add(childTransform.gameObject);
             }
 
             PrintArrayList("ItemHolders with FOREACH", _itemHoldersWithForEach);
         }
     }
     
     // Update is called once per frame
     void Update () {
     
     }
 }
 

In both cases, the Start method has two parts: "1." and "2.". The former uses "for" and the other uses "foreach" (in JavaScript, the "for" keyword is used as well).

You may have noticed I added a function called "PrintArrayList". After I ran the program, it showed these results:

alt text

Both cases printed all the children's names.

There you have it. I hope this helps.

By the way, I read in this post that it's recommended to use "List" instead of "ArrayList", but I didn't have time to make the changes:

Unless you have something forcing you to use ArrayList you should use a List instead. ArrayList is only included in C# for backwards compatibility

If you need to use "List", this post might help (it's in C#, though)


2015-04-d29-unity3d-forum-childrenlist-js-2.png (26.4 kB)
2015-04-d29-unity3d-forum-childrenlist-js.png (101.1 kB)
Comment
Add comment · Show 1 · 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 valtteri_m · Apr 30, 2015 at 01:14 PM 0
Share

awesome, thanks!

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

23 People are following this question.

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

Related Questions

Removing specific child objects 1 Answer

Make a simple tree 1 Answer

Find gameobject with name and if child of Player 1 Answer

Affect child's child from main parent's script? 2 Answers

Enable/Disable all children of parent with tag 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