I’m having problems accessing the child of the parent prefabs. Here’s the relevant parts of the code which works fine up to a point:-
public static GameObject[] weaponObject;
.
.
Awake(){
// weaponSlot is an empty gameobject to which weapons are attached to the player model
Transform weaponMount = transform.Find("Armature/hand_R/weaponSlot");
int count = weaponMount.GetChildCount();
weaponObject = new GameObject[count];
for(int cnt = 0; cnt < count; cnt++) {
_weaponObject[cnt] = weaponMount.GetChild(cnt).gameObject;
}
The problem is that weaponObject[cnt] only holds the parent prefab, so the children (the actual weapons) are not accessed. What can I add to this code to also find the children of weaponObject. Bearing in mind that weaponObject is a game object (prefab) containing other game objects and not a transform like weaponMount.
You’ll have to access the game object’s transform.
GameObject weaponChild = _weaponObject[cnt].transform.FindChild(“ChildName”).gameObject;
Thanks… This should work (although weaponChild should be arrayed) but I get null reference errors at runtime . As a recap:
weaponMount (empty gameobject) holds 3 gameobject prefabs. = Broken Axe, Short Sword, Stone Axe. These are correctly stored as…
_weaponObject[0] = Broken Axe.
_weaponObject[1] = Short Sword
_weaponObject[2] = Flint Axe
each has 1 further child so the expected result should be…
weaponChild[0] = axe handle
weaponChild[2] = s_sword
weaponChild[3] = f_axe
In this case, I know the name of each _weaponObject’s child but normally I wouldn’t. So I tried this adjustment of yor suggested code - but get the same error.
public static GameObject[] weaponObject; // has 3 children
[B]public static GameObject[] weaponChild;[/B] // has 1 child each
.
.
Awake(){
[I]// weaponSlot is an empty gameobject to which weapons are attached to the player model[/I]
Transform weaponMount = transform.Find("Armature/hand_R/weaponSlot");
int count = weaponMount.GetChildCount();
weaponObject = new GameObject[count];
for(int cnt = 0; cnt < count; cnt++) {
_weaponObject[cnt] = weaponMount.GetChild(cnt).gameObject;
[B]_weaponChild[cnt] = _weaponObject[cnt].transform.GetChild(0).gameObject;[/B]
// Debug.Log(_weaponChild[cnt].name);
}
}
Runtime error:
NullReferenceException: Object reference not set to an instance of an object
(wrapper stelemref) object:stelemref (object,intptr,object)…

[SOLVED]: Just as I posted this reply, I realised my error. I never instantiated _weaponChild.
_weaponChild = new GameObject[count]; fixed the problem.
I should get more sleep…