Object Group only spawns one object instead of random objects

How do I…

How do I spawn a random object from an Object Group?

I have an Object Group called Enemy that contains:

  • Goblin
  • Skeleton

I want to spawn enemies randomly from this group using events / spawner behavior

What is the expected result

When the game runs and enemies spawn:

  • Sometimes Goblin should spawn
  • Sometimes Skeleton should spawn
  • The spawn result should be random between the objects inside the group

What is the actual result

Only one object ever spawns — Skeleton.

Even though both Goblin and Skeleton are inside the Object Group, the game always creates Skeleton and never Goblin.

It does not appear to be random.

Related screenshots


I’ve attached screenshots of:

  • Object Group configuration (showing Goblin + Skeleton inside Enemy group)
  • The event used to spawn the object.

Gdevelop expects an object for the spawner. When you provide it with a group, it’ll take the first object it finds in that group.

I’m not sure how you can get it to randomise the object from a group using the periodic spawn action.

Instead of the periodic spawner action, you could create your own set of events to determine randomly which object to create. It’d involve a scene timer and a random number, and you could make use of the new ELSE event. This wouldn’t be onerous, and reasonably straight forward to do.

1 Like

Thanks for the clarification!

I tried replacing the periodic spawner with a scene timer and a random number, then spawning each enemy based on the result — and it works perfectly now.

Really appreciate the guidance :raised_hands:

1 Like

Can you post a screenshot of your events to help others?

As mentioned, you can use create objects by name using a group. Name the objects something like enemy0 and enemy1. Then use create object by name using “enemy” + ToString(Random(1))

For just 2 objects, the new else would also work.

Random(1)=0, Create enemy0
Else, create enemy1

1 Like

For this kind of problem, I usually use an extension so powerful that it has been banned from everything known.


The extension has been cast into Mordor, so you won’t be able to replicate it.
Only the legends of the past are aware of its existence.

3 Likes

Thanks for the suggestion!

I switched from using an object group to creating enemies by name with a random value.

Here’s my current setup:


I didn’t use the ELSE approach since this method already works well for my case, but I might use it later if I need more control.

Appreciate the help!:sparkles:

1 Like

I’ve tried searching for the extension I chose before, but it’s gone somewhere now :joy:, by the way thanks for your opinion!

1 Like

That extension had serious otherworldly consequences! There’s now a 25 percent chance of an alternative version of yourself being chased down the street by a ‘wolf’ or ‘whatever’

1 Like

whatever = Balrog of Khazad‑Dûm…you really don’t want to type that in. :fire: :fire: :firecracker:

By the way, mine was a joke about the fact that, for some reason, they removed one of the most useful extensions from the engine… even though it was literally two lines of JavaScript.

1 Like