- Home /
Filling array with children of multiple undefined gameobjects?
Hi! So I want to find all available transforms on a buttonpress, and put them in an array to randomise a spawning point.
I want to make a spawning randomiser of a pipelines, but I need the transforms of those children. Since it's not one GameObject I can't say 'FindComponentInChildren' or anything alike. All the children have tags to their name accordingly, so I can define when instantiating an object where it instantiated. here's my code:
GameObject[] HasTopTrans;
GameObject[] HasBottomTrans;
GameObject[] HasRightTrans;
GameObject[] HasLeftTrans;
public GameObject[] Arrrr;
public void OnClick()
{
//This would work, if the objects weren't children. It just collects all the transforms and puts
//them in one array. I'm using "using System.Linq" too.
HasTopTrans = GameObject.FindGameObjectsWithTag("Up");
HasBottomTrans = GameObject.FindGameObjectsWithTag("Down");
HasRightTrans = GameObject.FindGameObjectsWithTag("Right");
HasLeftTrans = GameObject.FindGameObjectsWithTag("Left");
Arrrr = HasTopTrans.Concat(HasBottomTrans).ToArray();
Arrrr = Arrrr.Concat(HasRightTrans).ToArray();
Arrrr = Arrrr.Concat(HasLeftTrans).ToArray();
}
Your question is not perfectly clear.
Which Transforms you want to collect? The children of the pressed button? Why
FindComponentsInChildren
($$anonymous$$d the 's') wouldn't work?Where is this script attached?
How and when is
OnClick
called?Are
Talu$$anonymous$$ium
,skewalu$$anonymous$$ium
and4wayalu$$anonymous$$ium
buttons?
@Hellium 1. The transforms of the prefabs, which get randomly spawned at any available spots on the buttonpress, I want to fill an array with all available transforms, then randomly pick one and spawn a pipe on there, move the pipe according to which tag the transform it spawned on has. FindComponentsInChildren wouldn't work since the prefabs aren't defined, unless there's a way to make an array, fill those with all the prefabs that are currently on the map and scan them for available transforms. 2. On the button, I'm only calling a void which does this on ButtonPress, it'd be illogical to put it on every prefab. 3. When I click the button. 4. They are the prefab pipes which are randomly generated as I described in 1.
I'm sorry if this is a second message, but my first reply didn't show up.
Ok, thanks for the precisions.
What does not work with your current code? GameObject.FindGameObjectsWithTag
is able to find every active object with the given tag, regardless of whether they are children.
You could also ask your spawner script to maintain a list of the children transforms when spawning a new pipe.
Answer by LucasMartin · Oct 06, 2021 at 01:48 PM
Supposing that the tranforms that you want to use belong to the objects "Up", "Down", "Left" or "Right", you could try something different:
Transform[] childTransforms;
Transform chosenTransform;
private void Start()
{
childTransforms = new Transform[transform.childCount];
}
public void OnClick()
{
for (int x = 0, i = transform.childCount; x < i; x++)
childTransforms[x] = transform.GetChild(x);
chosenTransform = childTransforms[Random.Range(0, childTransforms.Length)];
}
Then, all you have to do is to use the variable chosenTransform wherever you want.
Thanks for the reply, I used some of the code ideas in the reply I wrote in the reply of my original question, in this script however, it seems like you're getting all children in the whole 'world'?
Sorry for taking too long. My code gets all children of the object the code is attached to, so this might help you :)
Thanks! But I'd rather do the command line Hellium gave, since that way I don't need every clone to have a script!
Your answer
![](https://koobas.hobune.stream/wayback/20220613043902im_/https://answers.unity.com/themes/thub/images/avi.jpg)