Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by thenachotech1113 · Jan 23, 2014 at 03:54 PM · stringtagsswitch

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.

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Jamora · Jan 23, 2014 at 04:06 PM 1
Share

Have a look at what error CS0163 is on this $$anonymous$$SDN page. It should help you fix your problem

2 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image thenachotech1113 · Jan 23, 2014 at 11:02 PM 0
Share

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.

avatar image
2

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?");
         }
     }
 }
Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image thenachotech1113 · Jan 23, 2014 at 11:00 PM 1
Share

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.

avatar image thenachotech1113 · Jan 23, 2014 at 11:17 PM 0
Share

funny thinh, this one helped me with a problem i had afterwards.

avatar image moghes · Jan 24, 2014 at 08:23 AM 0
Share

A better solution than the switch :)

avatar image Max_Bol · Oct 19, 2018 at 05:08 PM 0
Share

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 :

  1. If it's a constant variable that doesn't change for an instance, use Switch().

  2. If it's a situation where you clearly aim toward specific goals, use Switch().

  3. If you use anything like Floats or anything that lack precision, use if().

  4. If you use a bunch of predeter$$anonymous$$ed values and want one exception when those values are not met, use Switch().

  5. If you need multiple conditions to be met at once and it's all or nothing, use if().

Your answer

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

22 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

String Scanning Switch Statement 1 Answer

Using the StartsWith function in a Switch Case Statement 1 Answer

Get text between two tags C# 1 Answer

Two cameras and switching 2 Answers

Does Unity's Mono do a reference compare in its string == operator? 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges