Cant seem to figure out why this is so difficult to implement( for myself at least ). Could a kind individual please provide a way.
A horrible best attempt would be.
public GameObject[] weaponGroup;
private int i;
void Awake()
{
Transform weapons = this.transform.Find("Weapons");
weaponGroup = new GameObject[weapons.childCount];
foreach (Transform child in weapons) {
weaponGroup [i++] = child.gameObject;
}
}
Thankyou.
That seems like it should work, but a better structure would be to:
- eliminate the private int i class variable
- instead of foreach(), use for (int i = 0; i < weapons.childCount; i++)
- remove the i++ within the weapon array reference brackets
- replace child.GameObject with weapons.GetChild(i);
So to sum it up:
public GameObject[] weaponGroup;
void Awake()
{
Transform weapons = this.transform.Find("Weapons");
weaponGroup = new GameObject[weapons.childCount];
for (int i = 0; i < weapons.childCount; i++) {
weaponGroup [i] = weapons.GetChild (i).gameObject;
}
}
Thankyou for the reply Kurt. Im still getting the same Object reference not set to an instance of an object. The script works but not for any Find(“String”) after Root_jnt.
Here is my Enemy Hierarchy. With the Script being on the “Enemy” transform.

Ah! That’s the problem: Find just searches at the given level.
You can solve this a couple of ways. One is you can write a recursive search, but that might find something called “Weapon” that isn’t the root of all weapons.
Another idea is to put a public Transform into your script that is “WeaponRoot”. This is more the “Unity Way.”
You would put this script at the root of your enemy prefab, and then drag the “Weapons” game object reference into it in the editor, and save off the prefab.
With that approach, your script looks like:
public GameObject[] weaponGroup;
public Transform WeaponRoot;
void Awake()
{
weaponGroup = new GameObject[WeaponRoot.childCount];
for (int i = 0; i < WeaponRoot.childCount; i++) {
weaponGroup [i] = WeaponRoot.GetChild (i).gameObject;
}
}
if you know the structure will be the same, you can do searches this way.
transform.Find("LeftShoulder/Arm/Hand/Finger");
need to update it obviously to reflect your structure
Thanks Kurt for the great example and the explanation. I didn’t know that about Find i somehow thought that it searched down until found(“String”).
Thanks James …
does Unity happen to have a “Where” feature. For example Where is “Weapon” == LeftShoulder/Arm/Hand/Finger/Weapon == Copy & Paste. (hmmm finger weapon lol).
Once again thank you both for your Time.RealTime.
You are welcome!
You can write a simple recursive Find() (or Where) function; the reason it’s not in Unity is probably because it’s so easy to write, and so many different ways to write it: does it return the first one? Does it return a list of all of them? What about a find function that finds everything that .StartsWith( “Weapon_”) for instance? Each of those things is a slightly-different problem.
Just google, you’ll find a recursive find for Unity probably on the wiki.
1 Like
Maybe something like this?
Found from here Find a child with a name, how to?? - Questions & Answers - Unity Discussions
Click for code
using UnityEngine;
public class TestFindChild : MonoBehaviour
{
void Awake()
{
GameObject[] weaponGroup = transform.FindChildNamed("Weapons").GetChildren();
foreach(GameObject obj in weaponGroup)
{
Debug.Log(obj.name);
}
}
}
public static class ExtTransform
{
//Even though it says FindChild, it will check the parent as well :/
public static Transform FindChildNamed(this Transform transform, string name)
{
if(transform.name == name) return transform;
for(int i = 0; i < transform.childCount; i++)
{
Transform foundChild = transform.GetChild(i).FindChildNamed(name);
if(foundChild != null) return foundChild;
}
return null;
}
public static GameObject[] GetChildren(this Transform transform)
{
GameObject[] children = new GameObject[transform.childCount];
for(int i = 0; i < children.Length; i++)
{
children[i] = transform.GetChild(i).gameObject;
}
return children;
}
}