- Home /
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^^
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);
}
Have you tried Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.Space)
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
Your answer
Follow this Question
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