Cant switch (integer) ?

Really? switch cant be used with an integer value , or maybe its cause my int is static? Either way it doesnt work , is this a case , or something else is wrong with my crap as usual , haha!

	switch(PlayerStatus.health){
		case (PlayerStatus.health >= PlayerStatus.maxHealth / 10 * 10) :
			GetComponent(GUITexture).guiTexture.texture = healthTexture[11];
			break;

i realise it could just as easily be reas as health == maxHealth , but thats not really the issue here, i dont think.

first of all it should be case (PlayerStatus.maxHealth) and then you need to make sure that this variable is static :wink:

I dunno if switching is the wisest choice in this case. Usually switching is someting used when you don’t want to nest a bunch of if statements… Or run multiple blocks of code for one case( leave out the breaks in between cases )…

As xpi said looks like your try to grab the variable that is static… If you using PlayerStatus multiply times grab the component of it. However you might of done that and just used a odd named convention…

Do something like

private PlayerStatus playerStatus;

void Awake 
{
     playerStatus = (PlayerStatus)FindObjectOfType(typeof(PlayerStatus)); 
}

Then you can access the script with playerStatus.health without it being static.

Actually, the problem i think is that switch didnt seem to be able to understand the comparsison of multiple values.

case (i < j i > b) :

if(i < j i > b)

works fine however , so i went with a buncha nested if’s.

Switch works only with constant-Values (At compile Time). Your PlayerStatus.health most probably is not constant.

Should give you that error.

In short, WTF?

You can only use a switch to compare one value to a bunch of fixed values. Anything more complex than that requires if statements.

You can only do stuff like this:

switch(a){
case 1: ... break;
case 2: ... break;
case 3: ... break;
default: ... break;
}

Which functions like this:

if(a == 1){ ... }
else if(a == 2){ ... }
else if(a == 3){ ... }
else{ ... }

You can play a bit with omitting break statements, so execution continues into the next case. If your switch is complex, this is likely to cause bugs.

Of course, you can use constants or values from an enum definition for the fixed values as well.

Switch is not what you want. you could only use switch in your case like this:

cases must be constant values like:

case 1:
case 20:

or if you have somewhere:
const int blah = 30;
case blah:

what you want is a if statement.

if( PlayerStatus.health >= (PlayerStatus.maxHealth / 10 * 10) )

thanks guys , sorted this out a couple days ago though. Thanks for the msdn tip, helpful place. Sort of, not quite so newb friendly , but it did help a little.

I even read about the switch statement , but i think i confused it with the if and it hung me up until i got the replies here. I R l33t.