Set Selection State w/ Code? amidoingitright?

I’m trying to figure out how to programmatically set a button to “selected”, it has proven to be harder than expected. Or at least, not very obvious.

I am currently calling eventSystem.SetSelectedGameObject.
For the arguments, I am giving it

  • my GameObject with the Button component,
  • new BaseEventData(eventSystem)

It seems to work, but feels unusual that i have to pass this second argument though. Is this right?

Are there any videos or tutorials anywhere that explain the “Selectable” stuff, and UI navigation? I saw this video, which explained a bit about the event system. Otherwise the docs that shipped with 4.6 are kind of light on this new UI navigation stuff. Or, I missed it.

1 Like

Well we haven’t finished the docs yet :slight_smile:

So this is correct currently, we are considering removing the second argument to the function as it doesn’t make much sense.

1 Like

I am just solving same thing. Why I have to change selected UI by code? Some type of drag and drop solution could be better. I pass as second argument null :slight_smile:

Thanks Tim. I agree on the second argument being confusing. It’s purpose is unclear to me. (Curious, what is it’s purpose?)

You can set the object that should be selected by default on the EventSystem component. It has a StartSelected property or something like that.

Of course, but I think change focus when I go to another menu. From Main menu to Options for example. I know that it is super simple script, but integrating this function could be better :slight_smile:

Yeah, the docs are light on the user interface navigation info.

I think it makes sense to NOT automatically select user interface objects, though. Otherwise you would have to write lots of annoying code to deselect things (if the behavior was not what you wanted.)

NOT automatically, but by user setting. For example as properties in Button in OnClick(). nGUI has something similar.

I’ve been playing around with this, I have my main menu and then I select options and I make the options canvas active.
(I’m using a gamepad controller btw)

I use this script as a component in my button:

public void Select(GameObject selection)
{
    eventSystem.SetSelectedGameObject(selection,  baseEventData);
}

then I call this from the button On Click () in the inspector.

What happens is that it select the new object but the old object stays selected forever and ever…

is this a bug?

ok, nevermind
I’ve found the error

I was activating/deactivating the object of the canvas that contains each submenu,
instead now I enable/disable the canvas component and it works fine