- Home /
EventSystem.SetSelectedGameObject Does not display highlight animations for my buttons
I have 3 buttons in a vertical layout in a panel. When I click a button on another object I want to enable the panel, with the middle button selected. This works perfect the first time around. I can use my up and down arrows to navigate the buttons, and all of them display their highlight animation. However, when I disable the panel and re-enter, glitchy things happen. Basically, sometimes one of the buttons are highlighted for a frame and then return to their idle state. When you highlight that button again it's highlight animation may work, at random chance. And sometimes just none of the highlight animations work.
Also, disabling and re-entering the panel a third time will guarantee that none of them work as far as I know. However, as I included in my code, I check for the highlighted object and they are highlighted as I want them to, they're just missing the animations.
I've tried with the Coroutine fix from another post but I'm probably doing something wrong.
This script is attached to the panel with the 3 buttons.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
public class Highlight : MonoBehaviour
{
public EventSystem eventSystem;
public Button myButton;
void OnEnable()
{
StartCoroutine (HighlightButton ());
}
void Update()
{
if (eventSystem.currentSelectedGameObject != null)
{
print (eventSystem.currentSelectedGameObject.name);
}
// This prints the correct buttons, but the highlight
// animations doesn't display most of the time.
}
void OnDisable()
{
eventSystem.SetSelectedGameObject (null);
}
IEnumerator HighlightButton ()
{
eventSystem.SetSelectedGameObject (null);
yield return new WaitForEndOfFrame ();
eventSystem.SetSelectedGameObject (myButton.gameObject);
}
}
SIdenote for moderators: I've already posted this question, 7 days ago, but it's still awaiting moderation and I've noticed that you removed post-moderation since then, so I'm reposting it.
Tested in Unity version 5.6.0f3
EDIT: Problem ended up being much more simple than I thought, it was simply an empty highlight state. Thanks a lot to @TonyLi !
Answer by TonyLi · Jun 16, 2017 at 02:51 PM
Call the button's OnSelect() method. This plays its transition.
// Deselect previous selection and play its deselect transition:
if (eventSystem.currentSelectedGameObject != null)
{
var previous = eventSystem.currentSelectedGameObject.GetComponent<Selectable>();
if (previous != null)
{
previous.OnDeselect(null);
eventSystem.SetSelectedGameObject(null);
}
}
// Select button and play its selection transition:
eventSystem.SetSelectedGameObject (myButton.gameObject);
myButton.OnSelect(null);
Changed my script like you said, and nothing changed. Same output as before; the buttons get highlighted like they should for the first time around, but after having the menu disabled and re-enabled, the highlights won't work for 2 of the buttons. The third time around, none of them work.
Here's my new script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
public class Highlight : $$anonymous$$onoBehaviour
{
public EventSystem eventSystem;
public Button myButton;
void OnEnable()
{
eventSystem.SetSelectedGameObject (myButton.gameObject);
myButton.OnSelect(null);
}
void Update()
{
if (eventSystem.currentSelectedGameObject != null)
{
print (eventSystem.currentSelectedGameObject.name);
}
// This prints the correct buttons, but the highlight
// animations doesn't display most of the time.
}
void OnDisable()
{
if (eventSystem.currentSelectedGameObject != null)
{
var previous = eventSystem.currentSelectedGameObject.GetComponent<Selectable>();
if (previous != null)
{
previous.OnDeselect(null);
eventSystem.SetSelectedGameObject(null);
}
}
}
}
I've also tried putting the OnDisable actions in the script that deactivates the object, so it happens before the deactivation of the menu. Still the same result. I could provide a youtube video of what's happening, but I'm not at home and I'm using a mac so I don't have a video recorder. Screenshots wouldn't be very helpful.
Any other ideas?
The script below works for me. Here's an example scene: TestHighlight_2017-06-16.unitypackage
using System.Collections;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
public class Highlight : $$anonymous$$onoBehaviour
{
public EventSystem eventSystem;
public Button myButton;
void OnEnable()
{
StartCoroutine(Select$$anonymous$$yButton());
}
IEnumerator Select$$anonymous$$yButton()
{
yield return null; // Wait 1 frame for UI to recalculate.
if (eventSystem.currentSelectedGameObject != null)
{
var previous = eventSystem.currentSelectedGameObject.GetComponent<Selectable>();
if (previous != null)
{
previous.OnDeselect(null);
eventSystem.SetSelectedGameObject(null);
}
}
eventSystem.SetSelectedGameObject(myButton.gameObject);
myButton.OnSelect(null);
}
void OnGUI()
{
// You can also inspect the EventSystem to see the current selection.
GUILayout.Label("Selection=" + eventSystem.currentSelectedGameObject);
}
}
Hey! Thanks for sticking up with me. Your scene works very well indeed, but here's a scene where I've changed so that Escape disables the menu, there isn't a "Back" button, and this seems to completely fuck everything up. I can't pin down the problem though, nor can I fix it. Any ideas?