- Home /
need help with a switch statement
hello everyone, i an trying to use a switch statement to compare tags to strings. anyway am getting an error and i cant realy explain why. here is the script
void Update () {
Ray cursorPos = cam.camera.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(cursorPos, out hit, range)){
blockSide = hit.transform.tag;
switch (blockSide)
{
case "cube_left":
Instantiate(availableBlocks[blockIndexSelected], new Vector3(hit.point.x, hit.point.y, hit.point.z)
, Quaternion.identity);
break;
case "cube_right":
Instantiate(availableBlocks[blockIndexSelected], new Vector3(hit.point.x, hit.point.y, hit.point.z)
, Quaternion.identity);
break;
case "cube_up":
Instantiate(availableBlocks[blockIndexSelected], new Vector3(hit.point.x, hit.point.y, hit.point.z)
, Quaternion.identity);
break;
case "cube_down":
Instantiate(availableBlocks[blockIndexSelected], new Vector3(hit.point.x, hit.point.y, hit.point.z)
, Quaternion.identity);
break;
case "cube_front":
Instantiate(availableBlocks[blockIndexSelected], new Vector3(hit.point.x, hit.point.y, hit.point.z)
, Quaternion.identity);
break;
case "cube_back":
Instantiate(availableBlocks[blockIndexSelected], new Vector3(hit.point.x, hit.point.y, hit.point.z)
, Quaternion.identity);
break;
default:
Debug.Log("da'hell am i supposed to do with this?");
}
(sorry its so long, but its pretty repetitive) anywhy, the mistake im getting is the following:
Assets/Scripts/Place_Blocks.cs(23,25): error CS0163: Control cannot fall through from one case label to another
i got absolutely no idea what is going on, if someone could help me find the problem, and if possible a solution it would be much apreciated. thanks a lot everyone.
Have a look at what error CS0163 is on this $$anonymous$$SDN page. It should help you fix your problem
Answer by moghes · Jan 23, 2014 at 04:06 PM
Simple add a break after the default:
just as you have done with the cases, the default is a case as well.
thanks a lot moghes, spent hours comparing this to a switch statement example, dont know how did i miss that. luckly there is an amaizing comunity out there willing to help.
Answer by KellyThomas · Jan 23, 2014 at 04:24 PM
Given that the code executed for all but the default case is identical I would recommend another approach entirely:
static string[] cubeFaces = new string[6] {"cube_left", "cube_right", "cube_up", "cube_down", "cube_front", "cube_back"};
void Update () {
Ray cursorPos = cam.camera.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(cursorPos, out hit, range)){
blockSide = hit.transform.tag;
if (cubeFaces.Contains(blockSide)) {
Instantiate(availableBlocks[blockIndexSelected], new Vector3(hit.point.x, hit.point.y, hit.point.z), Quaternion.identity);
}
else {
Debug.Log("da'hell am i supposed to do with this?");
}
}
}
thanks alot kelly, but i am going to do some variatons to each independent statement, i just wanted to get that switch statement down before that. thanks anyway i should have stated that.
funny thinh, this one helped me with a problem i had afterwards.
This solution works, but it's not "better" depending on what result is being aimed toward. There's an huge difference, in the long run, between using if() and switch() statements.
If you can use Switch(), it's actually better for the memory imprints since CPU simply ignore all codes in the unused cases of the switch statement. With that said, the Switch() statement gets completely deleted from the memory imprint once completed. So, if you call the same Switch in the same script twice with lots of different variables being changed by it result, each frame involve a totally new imprint.
The if() statement is actually fully stored into the CPU even if it returns false and it's kept until it's dumped. This can be useful if you make full use of all the possible states generated by the if within small amount of time such as things related to AI behavior or Inputs as those can change quickly over only a few frames. It's also obviously more open toward "average" results since you can use multiple conditions at once. With that said, as it's fully stored into the CPU, it always involve wastes. It's super small waste, but it's still waste and as Unity has a rather troublesome dumping system that re$$anonymous$$ds me of the Android's way of handling memory (keeping everything stored in the CPU until either told to clean up or until the memory is full), in the long run, there's quite an impact difference between both.
The way I tend to make each one being used is this :
If it's a constant variable that doesn't change for an instance, use Switch().
If it's a situation where you clearly aim toward specific goals, use Switch().
If you use anything like Floats or anything that lack precision, use if().
If you use a bunch of predeter$$anonymous$$ed values and want one exception when those values are not met, use Switch().
If you need multiple conditions to be met at once and it's all or nothing, use if().
Your answer
![](https://koobas.hobune.stream/wayback/20220613131227im_/https://answers.unity.com/themes/thub/images/avi.jpg)