Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by AhadKhan98 · Nov 07, 2015 at 08:36 PM · if-statementsif statementif-elseif else

How to put an if statement inside an if statement?

Hi. This is my code which does not seem to work, can you please tell me how to make it work?

 //                         if (Input.GetMouseButtonDown(0)){
 //                float ray = Camera.main.ScreenPointToRay(Input.mousePosition);
 //                RaycastHit hit;
 //                if(Physics.Raycast(ray,hit,Mathf.Infinity){
 //                    if (hit.collider.name=="hitbox1")
 //                        Application.loadedLevel("level1");
 //
 //                    if (hit.collider.name=="hitbox2")
 //                        Application.loadedLevel("level2");
 //                    }
 //                    if (hit.collider.name=="hitbox3"){
 //                        Application.loadedLevel("level3");
 //                }
 //                    if (hit.collider.name=="hitbox4"){
 //                        Application.loadedLevel("level4");
 //                }
 //                    if (hit.collider.name=="hitbox5"){
 //                        Application.loadedLevel("level5");
 //                }
 //                    if (hit.collider.name=="hitbox6"){
 //                        Application.loadedLevel("level6");
 //                }
 //                    if (hit.collider.name=="hitbox7"){
 //                        Application.loadedLevel("level7");
 //                }
 //                    if (hit.collider.name=="hitbox8"){
 //                        Application.loadedLevel("level8");
 //                }
 //                    if (hit.collider.name=="hitbox9"){
 //                        Application.loadedLevel("level9");
 //                }
 //                    if (hit.collider.name=="hitbox10"){
 //                        Application.loadedLevel("level10");
 //                }
 //                    if (hit.collider.name=="hitbox11"){
 //                        Application.loadedLevel("level11");
 //                }
 //                    if (hit.collider.name=="hitbox12"){
 //                        Application.loadedLevel("level12");
 //                }
 //                    if (hit.collider.name=="hitbox13"){
 //                        Application.loadedLevel("level13");
 //                }
 //                    if (hit.collider.name=="hitbox14"){
 //                        Application.loadedLevel("level14");
 //                }
 //                    if (hit.collider.name=="hitbox15"){
 //                        Application.loadedLevel("level15");
 //                }
 //                    if (hit.collider.name=="hitbox16"){
 //                        Application.loadedLevel("level16");
 //                }
 //                    if (hit.collider.name=="hitbox17"){
 //                        Application.loadedLevel("level17");
 //                }
 //                    if (hit.collider.name=="hitbox18"){
 //                        Application.loadedLevel("level18");
 //                }
 //                    if (hit.collider.name=="hitbox19"){
 //                        Application.loadedLevel("level19");
 //                    }
 //                    if (hit.collider.name=="hitbox20"){
 //                        Application.loadedLevel("level20");
 //                    }
 //                    
 //
 //                }





Comment
Add comment
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

3 Replies

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

Answer by Cepheid · Nov 07, 2015 at 10:33 PM

Hi there xyz567 is right you've missed some curly braces. Although might I suggest using a switch case statement rather than using so many If's? For example you could do this instead.

 if(Input.GetMouseButtonDown(0))
         {
             float ray = Camera.main.ScreenPointToRay(Input.mousePosition);
             RaycastHit hit;
             if(Physics.Raycast(ray,hit,Mathf.Infinity))
             {
                 switch (hit.collider.name)
                 {
                 case "hitbox1":
                     Application.LoadLevel("level1");
                     break;
                 case "hitbox2":
                     Application.LoadLevel("level2");
                     break;
                 case "hitbox3":
                     Application.LoadLevel("level3");
                     break;
                 case "hitbox4":
                     Application.LoadLevel("level4");
                     break;
                 case "hitbox5":
                     Application.LoadLevel("level5");
                     break;
                 case "hitbox6":
                     Application.LoadLevel("level6");
                     break;
                 case "hitbox7":
                     Application.LoadLevel("level7");
                     break;
                 case "hitbox8":
                     Application.LoadLevel("level8");
                     break;
                 case "hitbox9":
                     Application.LoadLevel("level9");
                     break;
                 case "hitbox10":
                     Application.LoadLevel("level10");
                     break;
                 case "hitbox11":
                     Application.LoadLevel("level11");
                     break;
                 case "hitbox12":
                     Application.LoadLevel("level12");
                     break;
                 case "hitbox13":
                     Application.LoadLevel("level13");
                     break;
                 case "hitbox14":
                     Application.LoadLevel("level14");
                     break;
                 case "hitbox15":
                     Application.LoadLevel("level15");
                     break;
                 case "hitbox16":
                     Application.LoadLevel("level16");
                     break;
                 case "hitbox17":
                     Application.LoadLevel("level17");
                     break;
                 case "hitbox18":
                     Application.LoadLevel("level18");
                     break;
                 case "hitbox19":
                     Application.LoadLevel("level19");
                     break;
                 case "hitbox20":
                     Application.LoadLevel("level20");
                     break;
                 }
             }
         }

It's just a suggestion but it could help you performance wise and it can also be easier to read in the long run.

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 AhadKhan98 · Nov 09, 2015 at 07:16 AM 0
Share

Works perfectly. Thank you so much.

avatar image
0

Answer by xyz567 · Nov 07, 2015 at 09:28 PM

You have missed curly brackets in the first two nested ifs. Change them to this:

if (hit.collider.name=="hitbox1"){ Application.loadedLevel("level1"); } if (hit.collider.name=="hitbox2"){ Application.loadedLevel("level2"); }

Comment
Add comment · 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
0

Answer by Statement · Nov 07, 2015 at 10:31 PM

Or replace all of them since the strings are so similar. Just replace hitbox with level.

 void Update()
 {
     if (Input.GetMouseButtonDown(0))
     {
         Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
         RaycastHit hit;
         if (Physics.Raycast(ray, out hit))
         {
             string hitbox = hit.collider.name;
             if (hitbox.Contains("hitbox"))
             {
                 string level = hitbox.Replace("hitbox", "level");
                 Application.LoadLevel(level);
             }
         }
     }
 }
Comment
Add comment · 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

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

34 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 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

If/else statement is always else 1 Answer

How to use IF in Unity for selection MyScript 1 Answer

how to make a succession(n+1,5n,etc) that work in if statement? 0 Answers

If Else problem 1 Answer

I want to create a button to check if one skybox is enable and change it to another one. 0 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