But in this case it breaks the script with out it. Put them in and it works. The only time I know where you don't need it is if there is only 1 thing to execute, but this script needs more than that. That -1 is bull, because It works now.
Not in your original code up top you don't. You have one for the Update and the switch but not the if. function Update () { if (numberOfBuildings > 0); buildingNumber = Random.Range (1,3); randomIndent = Random.Range (randomIndentLow, randomIndentHigh);
Actually burgunfaust has it right, his code change along with the added breaks fixes the original code, though I explained it better later, this is the correct solution. +1
Actually, the problem is the if statement, more specifically the semi-colon:
if (numberOfBuildings > 0);
which is interpreted as
if (numberOfBuildings > 0)
; // ie - do nothing.
The rest of the code is always executed, as it's outside the if statement. I also agree that it needs braces enclosing the logic after the if statement, unless js doesn't require this?
Fixed code (including break;s):
var building1 : GameObject;
var building2 : GameObject;
var building3 : GameObject;
var randomIndent = 0;
var randomIndentLow = 3;
var randomIndentHigh = -3;
var numberOfBuildings = 4;
var buildingNumber = 0;
var zchange = 10;
function Update ()
{
if (numberOfBuildings > 0)
{
buildingNumber = Random.Range (1,3);
randomIndent = Random.Range (randomIndentLow, randomIndentHigh);
switch (buildingNumber)
{
case (1) :
Instantiate (building1, transform.position + Vector3(randomIndent,0,zchange), transform.rotation);
numberOfBuildings--;
zchange +=10;
break;
case (2) :
Instantiate (building2, transform.position + Vector3(randomIndent,0,zchange), transform.rotation);
numberOfBuildings--;
zchange +=10;
break;
case (3) :
Instantiate (building3, transform.position + Vector3(randomIndent,0,zchange), transform.rotation);
numberOfBuildings--;
zchange +=10;
break;
}
}
}
Well, it WAS a problem. Now print the value of numberOfBuildings and make sure it is decreasing over time.
– flaviusxvii