# How do I spawn a object?

Get help using Construct 2

### » Sun Jan 04, 2015 6:51 pm

Every second a car spawns in my game but i have 5 different color cars and i want it to randomly spawn one every second how would i do that?
B
3
Posts: 9
Reputation: 193

### » Sun Jan 04, 2015 6:57 pm

floor(random(5)) will give you a number between 0 and 4 (theoretically you can also get 5 but exceptionally rarely.
then make your object spawn based on what the number is.
if it is just a different color car and no other difference, i'd personally just make 1 car with 5 animation frames, and just pick the frame based on the random number.

lots of different ways to do this.
R
B
7
S
3
G
1
Posts: 184
Reputation: 1,352

### » Sun Jan 04, 2015 7:18 pm

Random(5) will never produce a 5.

random(x)
Generate a random float from 0 to x, not including x. E.g. random(4) can generate 0, 2.5, 3.29293, but not 4. Use floor(random(4)) to generate just the whole numbers 0, 1, 2, 3.

https://www.scirra.com/manual/126/system-expressions
B
71
S
22
G
273
Posts: 3,822
Reputation: 150,787

### » Mon Jan 05, 2015 2:38 am

floor(random(5)+1 will result in 1,2,3,4,5

Better yet, skip the random and use

choose(1,2,3,4,5) = 1

this means 20% chance (or 1 out of 5 chance) of choosing 1st car, then do the same for other cars

choose(1,2,3,4,5) = 2
choose(1,2,3,4,5) = 3
choose(1,2,3,4,5) = 4
choose(1,2,3,4,5) = 5
B
16
S
7
Posts: 301
Reputation: 2,736