Find GameObjects and put them in an array

Hi
I have a empty object that is a parent to many different objects. Basicly imagine 10 swords,10 shields… I want to create a script that would always load whatever gameObject is a child of the first empty object akka parent. So if I want to add another sword I can just place the model under the parent and when I run the game it loads the new sword model instantly. The gameObjects would be stored in a array.

I tried various ways,but couldnt figure it out. The closest I got was storing 1 GameObject in the array.

I use java,so please post answers in java as well. Tnx

To loop through the children of a GameObject, just a foreach loop:

foreach(Transform child in gameObject.transform)
{
     Debug.Log(child.name);
}

So you could build your array or list from that.

I’m not entirely sure what you’re trying to do – your phrasing is a little confusing, possibly due to a language barrier – so if you try to clarify further I’d be happy to help more.

Tag your GameObject you want to be your parent and put all item as his childs, then tag the main Gameobject that is your Player
GameObject player =GameObject.FindGameObjectWithTag(“Player”);
GameObject parent = GameObject.FindGameObjectWithTag(“GAMEOBJECTTAG”);
parent.transform.GetChild(NUMBEROFCHILDINHIERARCHY-1).transform.parent = player.transform;

I think that is what you want

Tell me in your player do you have only Weapon child or you have more than one?

I am acutally have 4 different parts.
For example:
Swords parent has many different sword child models under it.
Shiedl parent has many different shield child models under it.

If I can establish how to get sword child models I will be able to do the others as well since they are the same.
I wanted to avoid tags.(can one object have more than one tag? for example tag player & sword)

What I want is that this script does everything automaticly so that I dont have to worry.about it later.
So that I can just add new models in game and the script automaticly loads the model and later automaticly ads it in menu where people can select it.

Btw,how would I load stats of the model from a txt file?
For example when I run the game the txt file is created and all the loaded models names are written on that txt file and then I just edit the file and add stats next to each name.

Hope it’s a bit clearer now :wink: