• 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
Question by Peremy · Oct 21, 2019 at 04:45 PM · scripting problembooleaninput.getkeytrue

how do i get a boolean to switch to true only after i've pressed a key three times?

total noob here, im learning unity for my interactive vis class for my masters degree. We've got a wee sample project due at the end of the week and im stuck on something integral to my diorama.

I need to make my boolean switch to 'true' after i've pressed the up key three times so I can then activate a bit of UI - you need to 'chant' a song three times to activate a mystic ruin... I'm completely stuck on how to do it and I know its a really basic question but I'd really appreciate a hand!

As it stands, I press the down key once, it activates a sound and the public bool switches to true. I need to press the down key three times, the sound play on each key down, and then the bool switch to true if that makes sense.

here's my code so far:

public class chanting : MonoBehaviour { public AudioSource chant;

  public bool isTrue;
  // Start is called before the first frame update
 
  void Update()
  {
      if (Input.GetKeyDown(KeyCode.UpArrow))
      {
          chant.Play();
          isTrue = true;
      }
 
  }

} appreciate any help!

Add comment

Comment

People who like this

0 Show 0
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
Best Answer

Answer by lgarczyn · Oct 21, 2019 at 06:28 PM

Simply have an int counter, that starts as 0. Everytime the key is pressed, increment the counter, then check if it is 3.

Once the counter reaches 3. Reset it, and set your bool to true.


If you want every key press to happen in a small amount of time, every time you get a key press, check that the current time is not too far away from the time of the previous key press.


 public bool isTrue;
 int pressCounter;
 float lastKeyPress;

 void Update()
 {
   if (Input.GetKeyDown(KeyCode.UpArrow))
   {
       if (Time.time - lastKeyPress < 1f) // no more than 1 sec between key presses
       {
           pressCounter++;
           if (pressCounter >= 3)
           {
                pressCounter = 0;
                [...]
           }
       }
       else
           pressCounter = 1;
       lastKeyPress = Time.time
   }
 }




Comment

People who like this

0 Show 0 · 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

Answer by Peremy · Oct 23, 2019 at 06:51 PM

Thank you that was really helpful! Im at a good stage now, but for some reason my counter counts up in twos instead of one by one - i made it public so i could test and it goes 2 - 4 etc. Any chance you have any insights?

 public class chanting : MonoBehaviour
 {
     public AudioSource chant;
 
     public bool isTrue;
     public int count;
 
     // Update is called once per frame
 
     void Start()
     {
         count = 0;
     }
     void Update()
     {
         if (Input.GetKey(KeyCode.UpArrow)) //When up key is pressed, chant soundbite sounds
         {
             chant.Play();
             count = count + 1;
 
             if (count >= 4)
             {
                 count = 4;
             }
             
             isTrue = true;
 
         }
 
     }
 }
Comment

People who like this

0 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 lgarczyn · Oct 24, 2019 at 06:35 PM 0
Share

Input.GetKey is true for every frame where the key is pressed, you need Input.GetKeyDown

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.

Update about the future of Unity Answers

Unity Answers content will be migrated to a new Community platform and we are aiming to launch a public beta later in June. Please note, we are aiming to set Unity Answers to read-only mode on the 31st of May in order to prepare for the final data migration.

For more information, please read our full announcement.

Follow this Question

Answers Answers and Comments

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

boolean not switching to false. 0 Answers

How do I Effect a whole Array of GameObjects' Scripted Boolean? 0 Answers

How to enable/disable an Icon depending on the bool state of a toggle. 0 Answers

Three button combination to open next scene 2 Answers

PLEAASEE I NEED HELP!!! How to turn Mesh Renderer of cubes on and off by pushing buttons? 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