- Home /
Switch-Case never reaches the default case.
I am making a game where you need to match up a falling ball to a matching color platform. I am using switch-case to detect which color is generated and which platform the ball hit.
void OnTriggerEnter2D (Collider2D col)
{
//if (col.gameObject.tag == "green" && color == 0)
//{
// addScore();
//}
//if (col.gameObject.tag == "red" && color == 1)
//{
// addScore();
//}
//if (col.gameObject.tag == "yellow" && color == 2)
//{
// addScore();
//}
//if (col.gameObject.tag == "blue" && color == 3)
//{
// addScore();
//}
//else
//{
// Destroy(this.gameObject);
// Debug.Log(true);
//}
switch (col.gameObject.tag)
{
case "green":
if (color == 0) { addScore(); }
break;
case "red":
if (color == 1) { addScore(); }
break;
case "yellow":
if (color == 2) { addScore(); }
break;
case "blue":
if (color == 3) { addScore(); }
break;
default:
Destroy(this.gameObject);
Debug.Log("TRUE");
break;
}
}
the if-else statements work just fine, but the switch-case "default" is never reached, it just skips over it. I looked at many examples of switch-case and I think I got the syntax right but it's not working.
As a side note, your if-statements are not attached, only your last one with "blue" is, so if its not blue, itll run that if-statement -- try putting all your if-statements together by using "else if", so your code looks more like:
//if (col.gameObject.tag == "green" && color == 0)
//{
// addScore();
//}
//else if (col.gameObject.tag == "red" && color == 1)
//{
// addScore();
//}
//else if (col.gameObject.tag == "yellow" && color == 2)
//{
// addScore();
//}
//else if (col.gameObject.tag == "blue" && color == 3)
//{
// addScore();
//}
//else
//{
// Destroy(this.gameObject);
// Debug.Log(true);
//}
And see if you get the same issues you do with the switch statemets.
@Dibbie Damn it! I did not even see that. I tried using the switch-case because my code would always go to the "else" block. Adding the else-if fixed that problem. God I'm such a noob. But thank you for your help. Post it as an answer and I'll mark it.
Answer by Laiken · Dec 16, 2016 at 06:38 PM
This happens because it will only go to the default state if the tag is not any of those four colors. Is there a tag like "pink" for instance? if the tag activated is "pink", it will go to the default state.
In your if/else code, you want the tag to be blue AND the color to be 3. If those 2 conditions are not met, it will destroy the object. But in you switch code, you only need the tag to be met, That's the difference.
for the switch to work it needs to be like this:
switch (col.gameObject.tag)
{
case "green":
if (color == 0) { addScore(); }
else
Destroy(gameObject);
break;
case "red":
if (color == 1) { addScore(); }
else
Destroy(gameObject);
break;
case "yellow":
if (color == 2) { addScore(); }
else
Destroy(gameObject);
break;
case "blue":
if (color == 3) { addScore(); }
else
Destroy(gameObject);
break;
default:
Destroy(this.gameObject);
Debug.Log("TRUE");
break;
}
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
how can i use spot light in canvas 2d 0 Answers