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 RussellFincher · Nov 23, 2015 at 12:48 PM · if else

How to turn on and off a bool with a button press

Dirt simple problem: I need a bool to switch back and forth when a single button is pressed. In my case the trigger on an Xbox controller.

  void Start () {
         crawlCheck = false;
     }
 
 void Update () {
         isCrawling = Input.GetAxis("Crawl Toggle");
 
         if (isCrawling == 1 && crawlCheck == false)
         {
             crawlCheck = true;
         }
 
         else if (isCrawling == 1 && crawlCheck == true)
         {
             crawlCheck = false;
         }
     }

I just need crawlCheck to change to false or true when the trigger is pressed and stay there until it's pressed again.

What I get is it switching from true to false and back as long as the trigger is held.

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 Dave-Carlile · Nov 23, 2015 at 01:20 PM

You can toggle the value of a boolean by using the "not" operator:

  • not true = false

  • not false = true

So you can write something like this to toggle it back and forth;

 if (your button is down)
 {
     crawlCheck = !crawlCheck;
 }



Comment
Add comment · Show 3 · 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 RussellFincher · Nov 23, 2015 at 02:51 PM 0
Share

Thanks, Dave! $$anonymous$$uch simpler. But this doesn't go in the update function, does it? If I put it there then crawlCheck reverses every frame that the button is held, right? I need it to just reverse once for every button press.

avatar image Dave-Carlile RussellFincher · Nov 23, 2015 at 03:06 PM 0
Share

You'd generally use this with Input.GetButtonDown which handles the "only return true once until the button is released and pressed again" functionality, so if you you can set this up as a button in the Input $$anonymous$$anager that would be ideal. If you must use GetAxis then you'd need to preserve the last state and only execute this on a state change.

 float lastCrawling;

 void Update()
 {
     // get the current state
     float crawling = Input.GetAxis("Crawl Toggle");

     // is it different from the last state?
     if (crawling != lastCrawling)
     {
         // preserve the new "last state"
         lastCrawling = crawling;

         // if the new state is "down" then do our down functionality
         if (crawling != 0)
         {
             crawlCheck = !crawlCheck;
         }
     }
 }


avatar image Hotpockets26 · Oct 03, 2018 at 02:12 AM 0
Share

Thank you very much Dave. I've seen you before on these forums you are very insightful, and you help me out a lot even thought you might not know it.

avatar image
0

Answer by Dudechester · Jul 15, 2018 at 05:38 PM

I'm a little confused by this thread guys, referring to the last message Dave, you said that you generally use the Input.GetButtonDown, but then you never used it in your example?

It may have made more sense if you did use GetButtonDown, because what does the GetAxis have to do with pushing a button? (I'm noob to code trying to learn) when you followed GetAxis, you did so with "Crawl Toggle" but this is a variable which you haven't declared previously? This code really confused me :)

What is the best way to do this please?

thankyou

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 Lolerooz · Jul 15, 2018 at 07:17 PM 1
Share

Im not sure what he meant either by using the axis, but if you were to use a GetButton command, you could do something like this

     public bool crawlCheck;
 
 
     void Start()
     {
         crawlCheck = false;
     }
 
     void Update()
     {
         if (Input.GetButton("Crawl"))
         {
             crawlCheck = true;
         }
         else
         {
             crawlCheck = false;
         }
     }
avatar image
0

Answer by K0ST4S · Apr 21, 2019 at 08:02 AM

Use properties instead of fields: public bool check; -> public bool check {get;set;} In the button component: Select "check" property in your script

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

9 People are following this question.

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

Related Questions

How to turn off/on a collider using a bool variable from another script 1 Answer

Help me replace tons of "if" on the adequate code. 0 Answers

Open and close a dooor 1 Answer

Text based game C#, if key selected>open door,Text based game, using boolean to pick up key and open door 0 Answers

Why is this fi statement not working?? 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