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
1
Question by CranberryHorses · May 28, 2018 at 01:56 AM · booleanif-statementsboolif statementkeycode

I'm having trouble setting a bool.

I'm trying to write a script where the player presses "Q" to grab an object. When the player presses "Q" again, the object is released. Here is a simplified version of my script:

 void Update()
     {
         if (Input.GetKeyUp(KeyCode.Q) && !isGrabbing)
         {
             GrabObject();
             isGrabbing = true;
         }
 
         if (Input.GetKeyUp(KeyCode.Q) && isGrabbing)
         {
             ReleaseObject();
             isGrabbing = false;         
         }

When I press "Q" the player grabs the object and immediately releases the object. It seems like both if statements are being activated at the same time. When I use two different Key Codes for grab and release, it works fine. This is why I don't think the bools are acting right. Any help is appreciated. Thank you.

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

2 Replies

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

Answer by QKlon · May 28, 2018 at 02:29 AM

Of course the logic is: Mark isGrabbing true, then check if it really is true and immediately make it false again.

Try something like:

 if ( Input.GetKeyUp(KeyCode.Q) )
   if( isGrabbing = !isGrabbing )
     GrabObject();
   else
     ReleaseObject();

Explaination:

  • You want only to do something, if Q is pressed.

  • Then you reassign 'NOT isGrabbing' (inverted) as new value to isGrabbing (i.e. you toggle the value first)

  • and immediately check the new state.

  • You call conditionally one of the methods.


As a bool value toggle you could also write isGrabbing ^= true. That's the XOR assignment operator, i.e. the short form of isGrabbing = isGrabbing ^ true.

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 CranberryHorses · May 28, 2018 at 03:22 AM 0
Share

This worked perfectly! Thank you so much!

avatar image
1

Answer by jim3878 · May 28, 2018 at 03:19 AM

you just need to add "else" like this:

          if (Input.GetKeyUp(KeyCode.Q) && !isGrabbing)
          {
              GrabObject();
              isGrabbing = true;
          }else  if (Input.GetKeyUp(KeyCode.Q) && isGrabbing)
          {
              ReleaseObject();
              isGrabbing = false;         
          }

because when you press "Q" when object is not grabbing it will match first expression

              if (Input.GetKeyUp(KeyCode.Q) && !isGrabbing)
              {
                  GrabObject();
                  isGrabbing = true;
              }

and set isGrabbing to true. Then ,program will go to second expression

  if (Input.GetKeyUp(KeyCode.Q) && isGrabbing)<<isGrabbing  was just set to true.
                  ReleaseObject();
                  isGrabbing = false;         
              }

It will match seconds expression too. so Adding "else" will make sure that "Input.GetKeyUp(KeyCode.Q)" will not run twice.

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

86 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

Related Questions

Mouse click IF not checking properly 0 Answers

What am I doing wrong with this bool? 3 Answers

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


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