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

Here’s a project I wrote a while ago:

[45465-2015-04-d29-unity3d-forum-childrenlist-js.png*_|45465]

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 :wink:

// 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*.gameObject.tag == "ItemHolder")*

itemHoldersWithFor.Add(childrenTransforms*.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();*

* // 1.*

* int childCount = childrenTransforms.Length;*

* if (childrenTransforms != null) {*
* for(int i = childCount - 1; i >= 0; i–) {*
_ if (childrenTransforms*.gameObject.tag == “ItemHolder”)_
_itemHoldersWithFor.Add(childrenTransforms.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:
[45474-2015-04-d29-unity3d-forum-childrenlist-js-2.png|45474]
Both cases printed all the children’s names.
There you have it. I hope this helps.
By the way, I read in [this post][3] 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][4] might help (it’s in C#, though)
*
*
[3]: http://answers.unity3d.com/questions/745558/how-to-read-properties-from-an-arraylist-of-object.html*_
_[4]: http://answers.unity3d.com/questions/527254/add-object-to-list-of-gameobjects-c.html*_