x


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!

more ▼

asked Apr 22 '11 at 07:58 PM

Muzz 1 gravatar image

Muzz 1
548 52 59 71

(comments are locked)
10|3000 characters needed characters left

3 answers: sort newest

break; at the end of each case!

more ▼

answered Apr 22 '11 at 08:09 PM

flaviusxvii gravatar image

flaviusxvii
3k 13 19 43

I feel so stupid...how did I miss that?

Apr 22 '11 at 08:24 PM Muzz 1

Actually, I tried that and it still doesn't work.

Apr 22 '11 at 08:32 PM Muzz 1

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

Apr 22 '11 at 08:53 PM flaviusxvii
(comments are locked)
10|3000 characters needed characters left

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

answered Apr 23 '11 at 09:11 AM

Seregon gravatar image

Seregon
131 6

I already said that.

Apr 24 '11 at 12:03 AM burgunfaust

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

Apr 24 '11 at 10:23 PM Seregon

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

Apr 25 '11 at 12:51 AM burgunfaust
(comments are locked)
10|3000 characters needed characters left

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

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

answered Apr 23 '11 at 12:08 AM

burgunfaust gravatar image

burgunfaust
260 5 7 17

Nope, you dont allways have to do that.

Apr 23 '11 at 08:35 AM OrangeLightning

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.

Apr 23 '11 at 01:00 PM burgunfaust

I've got braces...look the line below.

Apr 23 '11 at 05:07 PM Muzz 1

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

Apr 24 '11 at 12:03 AM burgunfaust

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

Apr 24 '11 at 10:57 PM Seregon
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x37
x20

asked: Apr 22 '11 at 07:58 PM

Seen: 904 times

Last Updated: Apr 22 '11 at 07:58 PM