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 /
avatar image
0
Question by suri44 · Jun 07, 2020 at 10:41 AM · togglebooleansgetkey

Toggle Boolean by Input.getKey like if pressed one time true next time false and so on.

Hello All,

I want a Toggle Boolean by using Input.getKey(i don't want it by keydown) like if pressed one time true next time false and so on. I have tried the code but it gives weird result. here is the code.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class NewBehaviourScript : MonoBehaviour
 {
     public bool suri;
 
     void Update()
     {
         if(Input.GetKey(KeyCode.A))
         {
                 suri=toggle(suri);
         }        
     }
      public static bool toggle(bool suri)
     {
      return !suri;
     } 
     
 }
 
Comment
Add comment · Show 7
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 bombombambam · Jun 07, 2020 at 11:02 AM 0
Share

what are you trying to achieve?

avatar image Hellium · Jun 07, 2020 at 11:02 AM 0
Share

GetKey returns true every frame while the key is pressed.

Why don't you want to use GetKeyDown or GetKeyUp which return true only the frame when the key goes down / goes up respectively?

avatar image suri44 · Jun 07, 2020 at 02:32 PM 0
Share

Thank you for your reply.

@Babish8 and @Hellium i am working on physical joystick which don't have button up function. It only have boolean, which onpress become true else false.

I want to have a toggle function on that button. so i cant use GetKeydown.

@AhmedElshazly i have tried this but it changes too fast and sometime don't work.

avatar image AhmedElshazly suri44 · Jun 07, 2020 at 02:59 PM 0
Share

try adding a delay and make sure you're not using static variables

avatar image AhmedElshazly AhmedElshazly · Jun 07, 2020 at 03:00 PM 0
Share

and also make sure to use Getkeydown

Show more comments

4 Replies

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

Answer by Hellium · Jun 07, 2020 at 03:16 PM

I haven't tried the following code

  public class NewBehaviourScript : MonoBehaviour
  {
      public bool suri;
 
      private bool isPressingKey;
 
      private bool IsPressingKey
      {
          get { return isPressingKey; }
          set
          {
              if(value == isPressingKey)
                 return;
 
             isPressingKey = value;
 
             // isSuri will be toggled when the key gets pressed down
             // if you want the boolean to be toggles when the key is released
             // change the condition to `if(!isPressingKey)`
             if(isPressingKey)
                 suri = !suri;
          }
      }
  
      void Update()
      {
          IsPressingKey = Input.GetKey(KeyCode.A);
      }
  }

However, it's tedious. Maybe the new Input System could solve your issue in a more elegant way.

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 suri44 · Jun 08, 2020 at 09:38 AM 0
Share

This doesn't work on my setup it's still giving issues of randomly true or false. Like I have have pressed one time and it quickly goes through true true and some time skip false and vice versa.

I needed something like event or state. I think but no idea how to use it.

avatar image Bunny83 · Jun 08, 2020 at 10:09 AM 0
Share

Well, this just manually emulates pretty much same behaviour as

 if (Input.GetKeyDown(KeyCode.A))
     suri = !suri;

The only difference would be when you use that inside FixedUpdate you get a different behaviour. So your code is essentially a GetKeyDown variant that works properly in FixedUpdate. Though in the original question he has the code in Update so this snippet works exactly like GetKeyDown as I shown above.


However since the actual goal is still unclear we essentially running circles here. The OP should clarify the question. Currently the question is not really clear and detailed.

avatar image Hellium Bunny83 · Jun 08, 2020 at 10:17 AM 0
Share

Indeed @Bunny83, the code I wrote tends to copy the behaviour of Input.GetKeyDown. This is what I understood from OP's question since he/she can't use Input.GetKeyDown directly but still wants the same behaviour.



@suri44 I don't really get the problem you are facing. The script I've provided works as follow:

  • Default value of suri is false

  • When pressing down the A key, suri turns to true and remains to true as long as you hold the key

  • When release the A key, suri keeps its true value

  • When pressing down the A key again, suri changes to false. Releasing the key does not change the value of suri

  • When pressing A a third time, suri is changed to true

avatar image
-1

Answer by AhmedElshazly · Jun 07, 2020 at 11:20 AM

Try Using this if(Input.GetKey(KeyCode.A)) { suri = !suri; }

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

Answer by Getsumi3 · Jun 07, 2020 at 03:31 PM

Here is my '5 cents'

BTW I'm not sure but I guess that if you use only GetKey (instead of GetKeyDown) it will keep calling the code inside untill you hold the key. so try it with GetKeyDown instead


 public bool isActive = false;
 
 private void Update()
 {
      if(input.GetKey(KeyCode.A))
      {
           isActive = !isActive;
           //more code here
      }
 }

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 Hellium · Jun 07, 2020 at 03:33 PM 0
Share

This will toggle the boolean every frame while the key is pressed.

avatar image Getsumi3 Hellium · Jun 07, 2020 at 03:38 PM 0
Share

well what else did you expected?

The OP wants with GetKey. I made an edit and mentioned about that.

Plus from persona experience I can't see anothe way to use Input.GetKey methods elsewhere except Update methods and Triggers

avatar image
0

Answer by suri44 · Jun 09, 2020 at 01:51 AM

Thanks Everyone, for all the solutions.

@Hellium you are right, your solution works special thanks to you.

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

132 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 avatar image avatar image

Related Questions

Render toggle help.. 1 Answer

How do I JavaScript to check if toggle is true? 0 Answers

Weapon Selector 1 Answer

Toggle camera sensitivity 1 Answer

How to make a Flashlight 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