Hello,
I have a for loop which instantiates 4 button prefabs in a UI panel with a Layout Grid Group and Content Size Fitter component attached.
The Player triggers the UI panel to become active when they walk over a certain area. The first button instantiated should be the one selected with the selected colour. What I need is to be able to control the UI using the keyboard arrow buttons and press space for the OnClick event.
The problem is that when the Panel is active and the buttons have been generated, the arrow buttons still only control the Player in the background.
How do I get the focus to be on the UI panel and stop the navigation on the Player?
Here is the script for instantiating the buttons on the panel:
private void PopulateUIPanel() {
UIController.instance.questionText.text = theQuestion;
for (int i = 0; i < answerList.Count; i++) {
GameObject newbutton = Instantiate(optionButton) as GameObject;
QuestionOptionButton button = newbutton.GetComponent<QuestionOptionButton>();
button.AnswerText.text = answerList[i].ToString();
button.TheAnswer = answerList[i];
if (answerList[i] == _equationAnswer) {
button.IsAnswer = true;
} else {
button.IsAnswer = false;
}
if (i == 0) {
EventSystem eventSystem = button.gameObject.AddComponent(typeof(EventSystem)) as EventSystem;
eventSystem.firstSelectedGameObject = button.gameObject;
}
newbutton.transform.SetParent(spacer, false);
}
}
This code works perfectly when using the mouse, just need to know how to use keyboard input. I added the EventSystem component to the first button instantiated in the hope this would do something, but it didn’t.
Many Thanks,
J