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
0
Question by pommerose94 · Jan 06, 2020 at 07:33 AM · buttonkeypress

control button with keypad

simply want an easy c# script to be able to click on my play or quit button or to click on it with keypad (for exemple quit my game by pressing the button or using escape, or play again with button and space) i have a script but he is not working

  public string inputName;
  Button buttonMe;
  // Use this for initialization
  void Start () {
      buttonMe = GetComponent<Button>();
  }
  
  void Update() {
      if(Input.GetButtonDown(inputName))
      {
          buttonMe.onClick.Invoke();
      }
          
 
  }

}

I started unity app 1 month ago and I LOVE IT^^

Comment
Add comment · Show 2
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 Ady_M · Jan 06, 2020 at 06:49 PM 0
Share

If you add the following code and press the button does the message appear in the console?:

 if (Input.GetButtonDown(inputName))
 {
     Debug.Log("Button pressed: " + inputName);
 }

avatar image ranch000 · Jan 08, 2020 at 10:50 AM 0
Share

Have you tried Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.Space)

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by JasonBennett · Jan 08, 2020 at 09:22 PM

Hello @pommerose94,

Are you using a button UI object? https://docs.unity3d.com/Manual/script-Button.html

If so, then you would need to create a public method in a script attached as a component to a game object in the inspector, then reference that game object in the OnClick section of the Button Component (second picture on that page). Hit the "+" to add a field for the reference.

As for the keypad input, you will need to declare

 public KeyCode keyForTheThingIWantToDo;

then within the Update() method:

 if (Input.GetKeyUp(keyForTheThingIWantToDo))
 {
     //put whatever you want to do in here
 }

then in the Inspector within the component for your script, select the key you want to use for that keycode (e.g. the "A" key, the "B", key, etc.)

Let me know if that doesn't make sense and I'll explain more in depth.

-Jason

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

142 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

Related Questions

Show GUI.Button press state by keyboard input? 1 Answer

GUI.Button's status react to keyboard event? 0 Answers

Numpad Keypress not being registered on mobile phone. 0 Answers

Is it possible to simulate a keypress/input in code? 5 Answers

Disable Sprite when keypress (Working Script, just need disble) 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