This post has been wikified, any user with enough reputation can edit it.
Question by
HavenGaming · Jun 20, 2016 at 04:10 AM ·
scripting problembutton
How do I script a cursor to scroll, and select a button with keys
I have created my menu and would like to be able to use the arrow keys to cursor through the buttons with a square highlighting button, and then select one of the buttons. How do I do this in c#? I have googled as many things as I can but no results for what I am looking for. Don't really process to go about doing this?
Comment
Best Answer
Answer by phxvyper · Jun 20, 2016 at 04:37 AM
Have a script that has an Array of all of your buttons that you want to scroll through. Keep track of which button the player has selected via an index, then update that index with Input in Update().
Sort of pseudocode:
class ButtonSelector : MonoBehaviour {
public Button[] Buttons;
public int SelectedIndex;
void Update() {
if (Input.GetKeyDown(Key.Right)) SelectedIndex++;
else if (Input.GetKeyDown(Key.Left)) SelectedIndex--;
else if (Input.GetKeyDown(Key.Enter)) Buttons[SelectedIndex].Press();
if (SelectedIndex < 0) SelectedIndex = Buttons.Length - 1;
else if (SelectedIndex >= Buttons.Length) SelectedIndex = 0;
}
}