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
– Apples_mmmmmmmmyep, 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
– Seth-BergmanI'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
– Nienipanini