- Home /
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
var vroomvroom:GameObject[]; <-- that's how it should be for JS. (Or more politically correct, Unity Script :P)
uh whoops I just wrote it wrong there, but how do I find them??
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
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!!
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
Answer by eleazar_dev · Apr 30, 2015 at 11:53 AM
Here's a project I wrote a while ago:
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:
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)
Your answer
Follow this Question
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