Auto-select and highlight first button of menu

Hello,

I have a simple menu with a question and two answers represented by two buttons. The two buttons are animated, so I set the transition type to “Animation”. On the first button I added a script that does “GetComponent().Select()” in OnEnable() so that button is selected directly when the menu appears (it is not the first menu so I can’t use the first selected value of EventSystem)

My problem is : the button is indeed selected (if I press the submit button, everything in OnClick() is called) but the state is set to normal instead of highlighted. If I move right, the second button gets highlighted, if I move left, the first button gets highlighted as it should have been from the start.

How can I force the first button to be selected ? Isn’t that the purpose of the Select() method ?

My button’s animator

2797376--202946--upload_2016-9-22_17-30-9.png

2797376--202948--upload_2016-9-22_17-32-31.png

Also, if I press submit with the button not highlighted (only selected), OnClick() is called but nor Disabled nor Pressed are sent to the animator.

Is this a bug in unity ?

Lastly, Idle and Active are animations with 0 length and transitions of 0.1 seconds, still, I can’t quickly change from one button to the other, often, it will take more than one second before the joystick movement is registered.

2797376--202949--upload_2016-9-22_17-35-57.png

BTW: I use a custom input module which is the standalone input module with everything mouse related removed.

As can be seen here, neither buttons are highlighted and it’s only when I press move right that “no” gets selected and highlighted.

I found a crappy solution. But first some background : so if you use the generated controller, everything works as it should. The problem here is that I had a “open” animation played before any other. For some reason, even if I set the open->idle transition to be on the condition of “normal” to be triggered, the transition is taken but the normal trigger isn’t reset.
The solution : start the animator, wait a bit, enable the button.
Still feels like a bug in unity, if someone could explain what really is going on, I would be very interested.

Maybe this will help you, add it to your menu script

        GameObject FTPno = GameObject.Find ("Button name (No)");
        if (FTPno) {
            EventSystem.current.SetSelectedGameObject (FTPno);
        }
2 Likes

It’s equivalent to
GetComponent<Selectable>().Select()
I use, but thanks :slight_smile:

Attach this to your first button.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class FirstButton : MonoBehaviour
{
  
    Button button;
    // Start is called before the first frame update
    void Start()
    {
        button = GetComponent<Button>();
        button.Select();
    }

 
}
3 Likes

This function only calls once though