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

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

Input.Get$$anonymous$$ey is true for every frame where the key is pressed, you need Input.Get$$anonymous$$eyDown

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

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

MoveTowards doesn't work correctly 0 Answers

How to change the Bool for only a single prefab GameObject creating with Instantiate? 2 Answers

Script Variable not transfering 0 Answers

boolean not switching to false. 0 Answers

How do I Effect a whole Array of GameObjects' Scripted Boolean? 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