- Home /
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!
Answer by flaviusxvii · Apr 22, 2011 at 08:09 PM
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.
Answer by burgunfaust · Apr 23, 2011 at 12:08 AM
Your problem is that you need to braces for your if statment.
if (numberOfBuildings > 0)
{
blah...blah....blah
}
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
Answer by Seregon · Apr 23, 2011 at 09:11 AM
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; } } }
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.
Your answer

Follow this Question
Related Questions
How do I enter buildings into my map? 1 Answer
Grid on Ship 1 Answer
Count Variable Acting Strange 1 Answer
Building a city 1 Answer
Why in the editor I see the scenes loading fine but in the build it's different ? 0 Answers