- Home /
EventSystem: how to highlight instantiated button?
Hi everyone.
I've structured a Dialogue System that will allow the player to respond NPC's by selecting between multiple answers, thus allowing for branching narratives. The answers the player can choose are instantiated GameObjects UI with a button attached to them that are generated from a list, and the player can navigate the answers using horizontal movement (Keyboard or Gamepad). The player can have a single or multiple answers to choose from, as the system is quite dynamic.
Overall it works well, but I'm stuck with an issue as I can't seem to find a way to highlight the first Gameobject to guide the Player in choosing an answer. Basically what happens is this: the player advance with the dialogue, then a response (or more than one) appear: none of them is selected/highlighted (Ive tried EventSystem.current.SetSelectedGameObject(button);).
The responses gets highlighted only if there are more than one, and only when the player uses the gamepad or keyboard to navigate them (in which case, the selected GameObject is always the last one).
Thanks in advance for your time and answers.
Answer by swanne · Oct 12, 2021 at 04:40 PM
Hey,
Are you generating the GameObject prefab from a list or array? If so could you utilise the Buttons 'tabIndex' and / or 'spriteState' on object [0]?
https://docs.unity3d.com/2018.2/Documentation/ScriptReference/UI.Selectable-spriteState.html https://docs.unity3d.com/ScriptReference/UIElements.Focusable-tabIndex.html
Hi Swanne,
thank you very much for your reply. I've tried to work a bit with both solution but I can't seem to find a way to implement them correctly. I found an interesting discussion here (https://forum.unity.com/threads/accessing-the-selectionstate-of-a-button.992123/) about accessing the Color Tint transitions, so I will probably try this workaround if I can't elaborate anything else.
Thank you for your time!
You are most welcome. Best of luck in finding a solution
Hi Swanne,
in the end it was a really noob error on my end. I was keeping in using the "EventSystem.current.SetSelectedGameObject(button);" inside the for loop that was adding the answer to the canvas, instead of outside the loop -_-. Now it works properly.
The issue with accessing the Color Tint transitions still remains, but in the end it was an error by me.
Thanks!