Problem with random range, not really random?

Hi all,

I have a problem with unity3d. I have a scene with 6 different “spawncubes” to each of this spawncube is the same script attached. Which generates a random number (with random range, between 0-100). This number i use to specify a delay time for the spawning action.

The problem i have now is that when the scene loads en the game starts all of these cubes spawn the very first “enemy” at the same time. So at the opening of the scene you get a flood with enemies and then the scripts runs normal and generates different spawning time so the enemies won’t fall down all together.

Anyone has any trought how i can fix this? It’s very strange because it only happens to the first group of enemies spwawned.

var vijand1 : GameObject;
var vijand2 : GameObject;
var vijand3 : GameObject;
var vijand4 : GameObject;

var minWait :float = 0.5;
var maxWait :float = 10;
var secs;

//in het begin is dit niveau van toepassing:
var huidigniveau: int = 1;




//bepalen welk niveau nu gehanteerd moet worden aan de hand van de tijd:
function Update() {
var secs : Timer_script;
secs = GameObject.Find("CameraEnGUI").GetComponent("Timer_script"); 

if (secs.timesec <= 59) {
//minder kan één minuut
huidigniveau = 1;
}

if (secs.timemins == 1) {
//één minuut of meer
huidigniveau = 2;
}

if (secs.timemins == 1 && secs.timesec >=30) {
//anderhalve minuut
huidigniveau = 3;
}

if (secs.timemins == 2 && secs.timesec >= 0) {
//twee minuten
huidigniveau = 4;
}

if (secs.timemins == 2 && secs.timesec >= 30) {
//vanaf 3 minuten:
huidigniveau = 5;
}

}




function Start () {
  SpawnRoutine();
  
}



function SpawnRoutine () {
  while(true) {
  
  //een zo random mogelijk nummer genereren:
  var spawnrandom: float = (Random.Range(0,100));


switch (huidigniveau)
{
case 1:
 //doe iets als niv1 is
 minWait = 0.5;
maxWait = 8;

if (spawnrandom > 0 && spawnrandom < 45) {
    Instantiate (vijand1, transform.position, transform.rotation);
    yield WaitForSeconds(Random.Range(minWait, maxWait));
    }

if (spawnrandom > 45 && spawnrandom < 90) {
    Instantiate (vijand2, transform.position, transform.rotation);
    yield WaitForSeconds(Random.Range(minWait, maxWait));
    }
    
if (spawnrandom > 90 && spawnrandom < 95) {
    Instantiate (vijand3, transform.position, transform.rotation);
    yield WaitForSeconds(Random.Range(minWait, maxWait));
    }
    
if (spawnrandom >= 95) {
    Instantiate (vijand4, transform.position, transform.rotation);
    yield WaitForSeconds(Random.Range(minWait, maxWait));
    }
 break;
 

case 2:
 //lvl1
 minWait = 0.5;
maxWait = 7;
if (spawnrandom > 0 && spawnrandom < 40) {
    Instantiate (vijand1, transform.position, transform.rotation);
    yield WaitForSeconds(Random.Range(minWait, maxWait));
    }

if (spawnrandom > 40 && spawnrandom < 80) {
    Instantiate (vijand2, transform.position, transform.rotation);
    yield WaitForSeconds(Random.Range(minWait, maxWait));
    }
    
if (spawnrandom > 80 && spawnrandom < 90) {
    Instantiate (vijand3, transform.position, transform.rotation);
    yield WaitForSeconds(Random.Range(minWait, maxWait));
    }
    
if (spawnrandom >= 90) {
    Instantiate (vijand4, transform.position, transform.rotation);
    yield WaitForSeconds(Random.Range(minWait, maxWait));
    }
 break;
 
 
case 3: 
 //lvl3
minWait = 0.5;
maxWait = 6;
if (spawnrandom > 0 && spawnrandom < 35) {
    Instantiate (vijand1, transform.position, transform.rotation);
    yield WaitForSeconds(Random.Range(minWait, maxWait));
    }

if (spawnrandom > 35 && spawnrandom < 70) {
    Instantiate (vijand2, transform.position, transform.rotation);
    yield WaitForSeconds(Random.Range(minWait, maxWait));
    }
    
if (spawnrandom > 70 && spawnrandom < 85) {
    Instantiate (vijand3, transform.position, transform.rotation);
    yield WaitForSeconds(Random.Range(minWait, maxWait));
    }
    
if (spawnrandom >= 85) {
    Instantiate (vijand4, transform.position, transform.rotation);
    yield WaitForSeconds(Random.Range(minWait, maxWait));
    }
 break; 
 
 
case 4:
 //lvl4
minWait = 0.5;
maxWait = 4;
if (spawnrandom > 0 && spawnrandom < 25) {
    Instantiate (vijand1, transform.position, transform.rotation);
    yield WaitForSeconds(Random.Range(minWait, maxWait));
    }

if (spawnrandom > 25 && spawnrandom < 50) {
    Instantiate (vijand2, transform.position, transform.rotation);
    yield WaitForSeconds(Random.Range(minWait, maxWait));
    }
    
if (spawnrandom > 50 && spawnrandom < 75) {
    Instantiate (vijand3, transform.position, transform.rotation);
    yield WaitForSeconds(Random.Range(minWait, maxWait));
    }
    
if (spawnrandom >= 75) {
    Instantiate (vijand4, transform.position, transform.rotation);
    yield WaitForSeconds(Random.Range(minWait, maxWait));
    } 
 break;
 
case 5:
minWait = 0.5;
maxWait = 2;
if (spawnrandom > 0 && spawnrandom < 20) {
    Instantiate (vijand1, transform.position, transform.rotation);
    yield WaitForSeconds(Random.Range(minWait, maxWait));
    }

if (spawnrandom > 20 && spawnrandom < 40) {
    Instantiate (vijand2, transform.position, transform.rotation);
    yield WaitForSeconds(Random.Range(minWait, maxWait));
    }
    
if (spawnrandom > 40 && spawnrandom < 80) {
    Instantiate (vijand3, transform.position, transform.rotation);
    yield WaitForSeconds(Random.Range(minWait, maxWait));
    }
    
if (spawnrandom >= 80) {
    Instantiate (vijand4, transform.position, transform.rotation);
    yield WaitForSeconds(Random.Range(minWait, maxWait));
    }
 break; 
}


  
  
}     
}

//naar het scherm printen welk niveau van toepassing is:
function OnGUI() {
 GUI.Label (Rect (10, 60, 100, 100), "huidig niveau is: "+huidigniveau.ToString());
 //GUI.Label (Rect (10, 60, 100, 100), "random getal is: "+spawnrandom.ToString());
}

try posting your script. You might get more help

yep, we'd need to see the relevant code.. But is it possible the first spawn comes BEFORE spinning the Random number? I'm guessing all enemies spawn right away the first time, b/c the var used for the Range is null to start.. make sure the Random.Range is set before the spawn

I've posted the script, thx both for the reponses! This script handles the min and max spawntime but also the kind of enemy that should spawn. All of this random. Now i also found out that enemy 3 (vijand3, sorry for the dutch) never gets spawned. But i don't know why

1 Answer

1

yep, as I suspected, you are Instantiating first, THEN waiting…
try switching the lines like this:

if (spawnrandom > 0 && spawnrandom < 45) {

    yield WaitForSeconds(Random.Range(minWait, maxWait));
    Instantiate (vijand1, transform.position, transform.rotation);      
    }

…etc
that may do it

one other note:

what happens if spawnrandom is EQUAL to 45, or 90?
you should account for this:

if (spawnrandom >= 45 && spawnrandom <= 90) {
    Instantiate (vijand2, transform.position, transform.rotation);
    yield WaitForSeconds(Random.Range(minWait, maxWait));
    }

as for #3 never spawning, it only has a 1 in 25 chance, that may be the only thing.. you can add a Debug.Log: var spawnrandom: float = (Random.Range(0,100)); Debug.Log(spawnrandom); // add this line then if you see it equal to the appropriate value, and it STILL doesn't spawn, check for error messages...

Thx a lot for all of these comments. I will look after that! Looking over the same code so long i don't see these (obvious) things.

Enemy 3 (vijand3) still doesn't get spawned i tried the Debug.Log to see which numbers are generated by the random.rage but nothing happens when the number is in between the two given values. No errors.. nothing

maybe it's got a weird transform.. check again, and when it's supposed to have spawned, check the hierarchy panel for the enemy, he may be spawning out of position somehow..

Thank you a lot for your help i forgot the put it as the right answer thanks.