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 /
  • Help Room /
avatar image
0
Question by Junglerally · Nov 06, 2021 at 09:39 PM · if-statements

If statements contradicting themselves?

I have a detection script for interacting with things in my game, but I think the IF statements mess with each other. I have tried many things, like separating them into different methods, changing the else statement that turns stuff off into more IF statements, and other things. Here is my code :

     void Update()
     {
         CauldronDetection();
         SpellBookDetection();
     }
 
     void CauldronDetection()
     {
         RaycastHit hit;
         if (Physics.Raycast(Camera.main.transform.position, Camera.main.transform.TransformDirection(Vector3.forward), out hit, 5))
         {
             Debug.DrawRay(Camera.main.transform.position, Camera.main.transform.TransformDirection(Vector3.forward) * hit.distance, Color.yellow);
             if (Input.GetKeyDown(KeyCode.E) && hit.collider.tag == "Cauldron" )
             {
                 //Cauldron
                 if (!cauldronMenuOn)
                 {
                     cauldronUI.SetActive(true);
                     cauldronMenuOn = true;
                     Cursor.lockState = CursorLockMode.None;
                     playerScript.canMove = false;
                 }
                 if ( cauldronMenuOn)
                 {
                     cauldronUI.SetActive(false);
                     cauldronMenuOn = false;
                     Cursor.lockState = CursorLockMode.Locked;
                     playerScript.canMove = true;
                 }
             }
         }
     }
     void SpellBookDetection()
     {
         RaycastHit hit;
         if (Physics.Raycast(Camera.main.transform.position, Camera.main.transform.TransformDirection(Vector3.forward), out hit, 5))
         {
             Debug.DrawRay(Camera.main.transform.position, Camera.main.transform.TransformDirection(Vector3.forward) * hit.distance, Color.yellow);
             if (Input.GetKeyDown(KeyCode.E) && hit.collider.tag == "Book")
             {
                 //Spell Select
                 if (!selectMenuOn)
                 {
                     selectUI.SetActive(true);
                     selectMenuOn = true;
                     Cursor.lockState = CursorLockMode.None;
                     playerScript.canMove = false;
                 }
                 if (selectMenuOn)
                 {
                     selectUI.SetActive(false);
                     selectMenuOn = false;
                     Cursor.lockState = CursorLockMode.Locked;
                     playerScript.canMove = true;
                 }
             }
         }
     }
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

1 Reply

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

Answer by Bunny83 · Nov 06, 2021 at 11:26 PM

There's no contradition. Your two if statements in line 16 and 23 are check one after the other. When "cauldronMenuOn" is false you would enter the first if statement. However inside that if statement you set cauldronMenuOn to true. So when the first if statement body is finished, the second if statement would be executed since the variable is now true. Instead of your second if statement you can use an "else" between your two bodies.


Though another way would be to get rid of the if statements completely and just do this:

 if (Input.GetKeyDown(KeyCode.E) && hit.collider.tag == "Book")
 {
     selectMenuOn = !selectMenuOn;
     selectUI.SetActive(selectMenuOn);
     playerScript.canMove = !selectMenuOn;
     Cursor.lockState = selectMenuOn?CursorLockMode.None:CursorLockMode.Locked;
 }

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 Junglerally · Nov 06, 2021 at 11:30 PM 0
Share

Thank you very much! This helped a lot.

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

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

RaycastHit2D ground detection 1 Answer

How do I run an animation then destroy a game object? 1 Answer

How to use or statements in an if statement. 1 Answer

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

How to cancel a if statement once touching a collider 1 Answer


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