- Home /
Changing the Select on Up / Select on Down in Navigation via script
I'm trying to find out how to change the Select on Up and the Select on Down under Navigation in the button script. I took a look at the script reference for UI.Selectable, but I couldn't find anything to change it, only to find other buttons. I suppose I could set the selected button manually in the script when the player presses up or down, but I would much rather find a way to change the navigation instead. The reason I need to do this is because the buttons are prefabs instantiated seperately based on an array, so the button above / below won't be necessarily be same (but they can be if need be), and when I tried to set the navigation in the prefabs(a long shot to be sure), they accepted the information and kept it, but still didn't navigate.
P.S I apologize for the run on sentences, it's a bad habit of mine.
Answer by Zelek · Jan 20, 2015 at 10:57 AM
Just ran into this same issue here. It looks like you just need to create a new Navigation struct and then assign it like so:
Navigation customNav = new Navigation();
customNav.mode = Navigation.Mode.Explicit;
customNav.selectOnDown = someSpecificSelectable;
_myObject.navigation = customNav;
Answer by DigitalDesignDude · May 22, 2020 at 06:49 AM
Adding on to what Zelek shared to make it easier for those less experienced.
using UnityEngine.UI;
public GameObject MyButton1;
public GameObject MyButton2;
public GameObject ObjectToHaveNewNav;
//Create a new navigation
Navigation NewNav = new Navigation();
NewNav.mode = Navigation.Mode.Explicit;
//Set what you want to be selected on down, up, left or right;
NewNav.selectOnUp = MyButton1.GetComponent<Button>();
NewNav.selectOnDown = MyButton2.GetComponent<Button>();
//Assign the new navigation to your desired button or ui Object
ObjectToHaveNewNav.GetComponent<Button>().navigation = NewNav;