- Home /
Setting a button navigation mode throug code
Hi guys! A have a problem in my game with UI buttons. For some reason they stay focused after one pressing, and after this pressing the "space" on the keyboard make them act like they are being pressed. I fixed this issue with setting buttons "navigation mode" to none in the editor.
The current problem is that i have some buttons that are created realtime during the game by code. How can i set those buttons navigation mode to "None" through code that instantiates it?
I tried ElementButton.navigation.mode = Navigation.Mode.None;
But unity says me: "Cannot modify the return value of 'Selectable.navigation' because it is not a variable"
What am i missing?
Answer by Rodoleia · Apr 05, 2016 at 11:32 AM
Ok, i found the answer by myself (hope it will be useful for someone) :
public Button ElementButton;
Navigation customNav = new Navigation();
void Start()
{
ElementButton = GetComponent<Button>();
customNav.mode = Navigation.Mode.None;
ElementButton.navigation = customNav;
}
You can store the original navigation object as a variable and then assign to it:
var navigation = button.navigation;
navigation.mode = Navigation.$$anonymous$$ode.Explicit;
You're close to this answer, but this will avoid the need to instantiate a new Navigation
object.
In the end you will need to reassign Navigation back to the Button either way, because Navigation is a struct. Otherwise, the change will be lost (the struct is just a copy).
Here's a one-liner solution: button.navigation = new Navigation() { mode = Navigation.$$anonymous$$ode.None };
In the end you will need to reassign Navigation back to the Button either way, because Navigation is a struct. Otherwise, the change will be lost (the struct is just a copy).
Here's a one-liner solution: button.navigation = new Navigation() { mode = Navigation.$$anonymous$$ode.None };