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 /
avatar image
0
Question by Nielo47 · Dec 23, 2018 at 05:34 PM · booleanif-statementsboolifwhats-wrong

What am I doing wrong with this bool?

Hi guys, so I'm trying to make a flashlight for my game and the logic is: if the Player presses "f" and the flashlight is off, it will turn on, and likewise in the other way around.

The code is like this:

 if (Input.GetKeyDown("f"))
         {
             FlashOn = true;
          }
 
 if (FlashOn && Input.GetKeyDown("f"))
         {
             FlashOn = false;
         }

When I run it and press "f" , the bool FlashOn is checked, but when I press it again it doesn't uncheck, like I intended ;-;

Is my logic wrong or am I making a coding mistake? If you guys could help me it would be much appreciated :)

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
2
Best Answer

Answer by pako · Dec 23, 2018 at 06:01 PM

  if (Input.GetKeyDown("f")) //first check the input, then act on it
          {
             if (!FlashOn) //if off
             {
                FlashOn = true; // turn on
                
             {
             else // if on
             {
                FlashOn = false; //turn off
             }
           }





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
2

Answer by Frepperino · Dec 23, 2018 at 06:02 PM

When you press "f" the script is first told to set FlashOn to true. Then the script is told to set FlashOn to false if the "f" key is pressed (which it is) and if FlashOn is true which it is since you just told it to. This leads to the bool always being false since every time you press the button it is first set to true and then set to false. I do not understand why it would always be true, like you mentioned. Either way. One way to fix this is to make the first if statement an else if statement of the second if statement. What I mean:

 if (FlashOn && Input.GetKeyDown("f"))
         {
             FlashOn = false;
         }
         else if (Input.GetKeyDown("f"))
         {
             FlashOn = true;
         }

But since you want to toggle the flashlight I would recommend you to also just toggle the bool. This is what I mean by that:

 if (Input.GetKeyDown("f"))
         {
             FlashOn = !FlashOn;
         }

Now, each time you press "f" the bool will be set to what it is not.

Comment
Add comment · Show 2 · 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 pako · Dec 23, 2018 at 06:10 PM 0
Share

@Frepperino although your code at the top is correct, it is unnecessarily checking input twice. However, your code at the bottom is the simplest and most efficient solution +1.

avatar image Frepperino pako · Dec 23, 2018 at 07:03 PM 0
Share

@pako That is indeed true, if you're going to use input several times for different purposes your solution is definitely the superior one. +1 to you too!

avatar image
0

Answer by Nielo47 · Dec 23, 2018 at 07:23 PM

Thank you very much guys, it worked! :)

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

100 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

Related Questions

How Do I check if a Bool was True a few moments ago 2 Answers

Could use some help on making my runaway/chase/follow/patrol script cooperate with each other 1 Answer

Scavenger Hunt List Bools Question 1 Answer

I'm having trouble setting a bool. 2 Answers

c# Ignoring conditional statement? 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