I have 8 buttons on screen and numbered 1 - 8. I want to get these buttons to recognize a key press so that when I press 1, button 1 detects the key press. Could someone point me in the right direction?
Is there a particular reason you need the button to detect the key press?
I assume you have an event trigger set up on the button to call some method that handles your button click - wouldn’t this be enough in your input manager?
if (Input.GetKeyDown("1")) {
//Code here to call the same method that pressing Button1 calls
}
I’m trying to create my own MMO style skill bar which is why I need buttons that can detect key presses.
Right.
So what you can do is have a method called “CastSpell” (or whatever) that get’s called when the key is pressed as well as when the button is clicked. Something like this:

Each one of those buttons has this in the inspector

And the Slot script looks like this:
using UnityEngine;
using System.Collections;
public class Slot : MonoBehaviour {
public void MouseDown() {
Debug.Log ("Call a method that will handle this click");
}
}
Now I assume you have some sort of Input Manager. I usually have an empty gameobject that deals with input using a script. Here’s what the InputManager’s script would have to be:
using UnityEngine;
using System.Collections;
public class InputManager : MonoBehaviour {
// Update is called once per frame
void Update () {
if (Input.GetKeyDown("1")) {
Debug.Log ("Call a method that will handle this key press");
}
}
}
And that’s it. All you’d have to do is change those Debug.Log statements to something like “Player.CastSpell(1)” or whatever - that part depends on your game.
I guess I don’t really understand why you need the key press to call the button’s OnClick method… you could instead make the key press call whatever method handles the Button’s OnClick. Regardless, these are small details. I think the main point here is that you realize you can just code the key press to call some method that the Button’s OnClick also calls!
Let me know if that helped at all!
How would you make a key press call the methods that handles a button’s onclick? Is it less resource intensive?
Edit: Nevermind, I figured it out
What was your solution? I don’t see how my last post didn’t answer your question though…
Oh, in update(), I have code checking for a keypress and if pressed, call the onclick function I made.
Okay, so, exactly what I was trying to tell you to do…
I guess as long as you figured it out…