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
2
Question by Namerian · Oct 11, 2016 at 12:51 PM · c#unity 5uiinput

[5.4.1f1] After clicking on a Button, pressing Space fires the Button.onClick event

Like the title says.

I have a pause Button that pauses/unpauses the game. Pressing the Spacebar has the same effect.

It works fine with the button. The Spacebar works too, until I press the Button once. From that point on, everytime I press the Spacebar the Button fires its onClick event.

This is how I listen to the Button: _PauseButton.onClick.AddListener(gameManager.OnPauseButtonClick);

Here is the relevant code handling the Input: void Update () { HandleInput ();

     if (!_Paused) {
         _DayTimer += Time.deltaTime;

         if (_DayTimer >= DAYLENGTH) {
             _DayTimer -= DAYLENGTH;
             _Days++;
             _DateView.SetDate (_Days);
         }
     }
 }

 public void OnPauseButtonClick ()
 {
     HandlePausing ();
 }

 private void HandleInput ()
 {
     if (!_SpaceKeyDown && Input.GetKeyDown (KeyCode.Space)) {
         _SpaceKeyDown = true;
         
     } else if (_SpaceKeyDown && !Input.GetKeyDown (KeyCode.Space)) {
         _SpaceKeyDown = false;
         HandlePausing ();
     }
 }

 private void HandlePausing ()
 {
     Debug.Log ("GameManager:HandlePausing:called! Pause=" + _Paused);

     if (!_Paused) {
         _Paused = true;
         _DateView.SetPause (true);
     } else {
         _Paused = false;
         _DateView.SetPause (false);
     }
 }

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 saschandroid · Oct 11, 2016 at 01:13 PM

I guess that your button is still selected and because of this 'fired' if you press the space bar. If that is the problem you could try to 'deselect' the button after it is clicked by:

 using UnityEngine.EventSystems;
 
  public void OnPauseButtonClick ()
  {
      HandlePausing ();
     // Deselect everything ... probably you have to move this to another place.
     EventSystem.current.SetSelectedGameObject(null);
  }

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 Namerian · Oct 11, 2016 at 03:13 PM 0
Share

Thanks, that did it, although it feels a little cheesy.

avatar image
2

Answer by saschandroid · Oct 11, 2016 at 05:22 PM

There is another solution for this: In the inspector go to the Button script component and set Navigation to None. This should deselect the button after a click automatically.

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 Gruffy · May 27, 2021 at 03:04 AM 0
Share

I believe this to be a sure answer alongside the currently accepted answer as this offers an approach to undertake a resolution via the Unity Editor and in the Button component inspector which offers non-technical users an approach to resolving this issue without additional code complexity.

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

6 People are following this question.

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

Related Questions

How to create a turn based battle system? 3 Answers

[C#] Unity asks for a } for a reason I don't know ( '}' expected ) 1 Answer

how to track UI buttons on Image targets 0 Answers

C# UI list item drag onto 2d sprite in world space? 1 Answer

Gamepad menu navigation with uGUI not working as expected. 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