[Solved] How to creat random objects


Suppose I have 5 sprite object (A,B,C,D,E).
I want to create sprite randomly when I press left mouse button.But how can I make events to choose the objects? And I want the sprite’s name (A,B) only spawn only once. And I don’t want to create a object over another object. But I don’t know how to do that. Any help will be great.Thanks in advance.


I would use JavaScript.



I think its possible with Pick random object command…Please help me…you know I don’t know anything about coding.


May be make a variable “Random” !
On click trigger once- set value of “Random” to random in range of 1-5 ! Then if value of random =1 and no of obj 1 is <1 (if you want that object to spawn only once during the scene) --> create obj 1. if 2 and no of obj2 <1 then create obj 2 …! And like this! I think this is what you wanted and hope this works!:slight_smile:


Random with javascript! first create a variable with a random value:
var rand = Math.random();
Then use it however you want. To set it to a gdevelop variable, use
runtimeScene.getVariables().get(“Variable_name”).setNumber(rand); I think.
Change Variable_name with the variable name of your choice. The variable will be a scene variable.
The number will be a random number between 0 and 1.
Examples number generated with Math.random():
Other possibility:
var rand = gdjs.randomInRange(minimumnumber, maximumnumber);
Replace maximum and minimum number to generate a random number between the two given.
gdjs.randomInRange(1, 7) will give either 1,2,3,4,5,6 or 7.
Use the runtime scene statement above to set it to a gdevelop variable.


Thanks a loooooooooooooooooooot! I have solve the random spawn system but I didn’t understand this quout…can you tell me the action and condition names?


Thanks it was helpful.


Value of scene variable =…
No of objects on scene =…


Bro sorry! I failed . Now I can create object Randomly. But Object (A & B) is not creating only once.
File link. Please help.



Help Please!


Send screenshot of the events



If you want something to happen only once, use trigger once


Do you want it to be created once per click (1)or once in the entire game/match(2)?
If you want to do (1) the put: Trigger once condition
If you want to do (2):
If the variable is 1 and you have to create obj 1:
Value of Scene variable is 1 &
No of *obj 1 * is <1
Trigger once---------->create obj 1

Note:This one will create no objects if you get variable 1 again and you already have obj 1 created before(you will have to put seperate events for that)
And if you dont want the objs to pile up on each other then just put a condition :The cursor or touch is on object (obj1,2…) and invert the condition

Hopefully this helped. :grin:

And I am sorry I dont have pc right now so I could not check the file you sent


Hey Thanks …And sorry for becoming so dumb. I am having this problem When value variable is 1 again no object is created.I want what action should I use when Number of obj A & B is = 1 and Value of variable is 1 & 2.


Make the variable Random in range again!! Do that cycle continues till you get 3 or 4 or…


I did that but sometime I click buttone but no object created.


Send screenshot of your event!
And do that event that i told you for every single object not just one!


You can Ignore the red area.
I have a object group named Random and a global variable named Random.

You can have the file here


I dont know what’s wrong ! Sorry but I think you should have got your result right if you did what I said! And again I am not on pc so I cant help much! Sorry