Choosing random child actor component

I am making an endless runner and I want to spawn a random obstacle from an array but I’m not sure how. I tried making an array of all my obstacles, which are blueprint classes, and then choose a random one from that array and spawn it but I can’t actually add anything to the array.

My obstacles don’t have a parent. They are separate blueprint classes.

Hey -

Looking at your screenshot - you have an array of transforms rather than an array of actors. Assuming all of your obstacles have the same parent class, you can create an array who’s type is the parent class. You can then fill the array elements with the obstacle blueprints and use a SpawnActor node to spawn different actors from the array.

Cheers

If they are all based on actor, then you can use the Actor class for your array.

I’ve done what you said and it does choose a random obstacle but the problem is that instead of putting in of of the three possible spawn locations it puts it in all three.

Are you adding these nodes in the level blueprint or in another actor blueprint? If this is happening in another actor, how many instances of that actor are in your scene? When I added the spawn logic shown in your screenshot I was only getting one element of my obstacle array created at one of the possible transforms from my Spawn Points array. If you are getting multiple unnecessary spawns, you can add a Do Once node after Begin Play to ensure it only spawn a single actor.

Well my blueprint for spawning the obstacles is in the floor tile. At one time there are 10 floor tiles (random ones but are children of the main floor tile which is where the blueprint for spawning the obstacles is)

I used the Unreal Engine tutorial on how to make an endless runner if that helps.

I’ve made it work, even if it isn’t the most efficient way it works.