Where am I going wrong (switch statement)

What's wrong with my code? It just keeps on generating buildings, doesn't stop.

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;
case (2) :
Instantiate (building2, transform.position + Vector3(randomIndent,0,zchange), transform.rotation);
numberOfBuildings--;
zchange +=10;
case (3) :
Instantiate (building3, transform.position + Vector3(randomIndent,0,zchange), transform.rotation);
numberOfBuildings--;
zchange +=10;

}
}

Thanks!

3 Answers

3

break; at the end of each case!

Well, it WAS a problem. Now print the value of numberOfBuildings and make sure it is decreasing over time.

Your problem is that you need to braces for your if statment.

if (numberOfBuildings > 0)
{
blah...blah....blah
}

Nope, you dont allways have to do that.

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

Thanks Seregon.

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;
}
}
}

I already said that.

Yes, and I agreed. You didn't mention the surplus semi-colon, though you did fix that in your code.

Well if he had only one line to execute in his if statement, then the one semicolon would have been right.