- Home /
How to Spawn multiple game object one by one in random order
i am creating a game where 3 random game objects ( green cube, red cube and blue cube) spawn one by one in random order. my approach to this is creating a RNG value for each game object ("if RNG <= 5 redcube will spawn") but that causes the spawn to either ( just to know each cube has a limited number to spawn like redCube=10 GreenCube=3 BlueCube=7):
1-overlap with another object.
2- spawn all 3 at once
3- take too long to spawn cause of RNG.
i have been stuck on this issue for months and i dont know what to do anymore, it is a core feature in my game and it would be nice if someone experienced could help me with my issue.(i am using invokerepeat for the spawning).
i want the game objects to spawn even if one of them have finished spawning. E.g redcube gameobject is 0 but greenobject and blueobject should still spawn till all have finished.
its not an endless game.
To confirm, you'd like to configure some number of colored cubes to spawn in a randomized order?
Are these on independent timers or do you expect them to all happen in evenly spaced out intervals?
Answer by dspruill · Sep 02, 2018 at 01:39 AM
Can you give me a little bit more to go on about how exactly you are spawning?
For instance, I suggest having a single script controlling the spawning of all 3 from there, I recommend a for loop (please excuse the psuedo code)
int numRed = 10
int numBlue = 7
int numGreen = 3
//spawns 10 objs
int rand
for(/*total spawn amount*/) {
rand = Random.Range(0, numRed + numBlue + numGreen);
if (rand< numRed){
//spawn Red
}
else if (rand < numRed + numBlue){
//spawn blue
}
//...etc...
}
This should at least give you a start on the problem :). Let me know what else you need
@dspruill this is my current spawn code:
private int FirstBubbleLimit = 0;
private int SecondeBubbleLimit = 0;
private int FirstBubbleRNG;
private int SecondBubbleRNG;
void Start ()
{ InvokeRepeating("SpawnFirstBubble", 3f, 3f);
InvokeRepeating("SpawnSecondBubble", 5f, 2f);
}
void SpawnFirstBubble ()
{
FirstBubbleRNG = Random.Range (1, 100);
if (FirstBubbleRNG <= 50)
{
Vector3 spawnPosition = new Vector3 (Random.Range (-2.3f, 2.3f), 6, 5);
Quaternion spawnRotation = Quaternion.identity;
Instantiate (Bubbles[0], spawnPosition, spawnRotation);
FirstBubbleLimit++;
if (FirstBubbleLimit == 5)
{
CancelInvoke ("SpawnFirstBubble");
}
}
}
void SpawnSecondBubble ()
{
SecondBubbleRNG = Random.Range (1, 100);
if (SecondBubbleRNG <= 100)
{
Vector3 spawnPosition = new Vector3 (Random.Range (-2.3f, 2.3f), 6, 5);
Quaternion spawnRotation = Quaternion.identity;
Instantiate (Bubbles[1], spawnPosition, spawnRotation);
SecondeBubbleLimit++;
if (SecondeBubbleLimit == 10)
{
CancelInvoke ("SpawnSecondBubble");
}
}
}
the intention for this code is to spawn 2 or more game objects till a certain amount $$anonymous$$g 10 redobjects. while the blueobjects will keep spawning till it runs out too. but the problem is that its too random i wonder if there is a better way to control the way it spawns
I'm sorry, but I still don't entirely get what you're trying to do. I also just realized that I left out a part in my pseudo code above. At the end of each if statement, decrement the num color. For ex:
if (rand< numRed){
//spawn Red
numRed--;
}
thank for replying and taking your time to help me.
what i am trying to do is to :
spawn 3 game objects, in a random order ( so if player restarts the level it would be different every time) but they have to be a limited number of game objects so that the game can end. it isn't an endless game.
The problem: with my code it spawns the game objects in random order, but because i am using three different Random Values at the same time for each game object. Sometimes it spawn at the same time or take too long to spawn because of random numbers.
i prefer the game object to spawn 1 by 1 and stop after a certain amount.
ty
i have seen the code you provided, the only part i dont understand is :
//spawns 10 objs
int rand
for(/*total spawn amount*/) {
rand = Random.Range(0, numRed + numBlue + numGreen);