- Home /
Unity 4.6 GUI select button via script
Hi. So I have a button which disappears when its clicked upon and other button shows up in the same place and I'd like it to be automatically selected. I would write a function for it but I don't know if there's a command that does that. The documentation is non existing yet since it is a beta release and those few video tutorials mention only the editor functions...
Thanks for any tips
EDIT - there should be such a command since you can set "First Selected" in the EventSystem
Answer by AyAMrau · Aug 28, 2014 at 08:14 PM
There are public functions on the event system:
public void SetSelectedGameObject(GameObject selected, BaseEventData pointer);
public void SetSelectedGameObject(GameObject selected);
and the EventSystem type is under: UnityEngine.EventSystems.EventSystem
Also there are beta docs installed locally with this version:
...[Unity Installation Folder]\Editor\Data\Documentation\html\en\index.html
This looks really useful. I am stuck now on what to pass in for the pointer argument?
@Humph you can just make a new one, it takes the Event system as an argument but you should already have a reference to it since you are calling SetSelectedGameObject:
eventSystem.SetSelectedGameObject(gameObject, new BaseEventData(eventSystem));
I have a small set of scripting examples at: https://github.com/AyARL/UnityGUIExamples
But if you did not have a reference you can grab the current using EventSystem.current
Non performance caring example:
EventSystem.current.SetSelectedGameObject(GameObject.Find("LookForName"), new BaseEventData(EventSystem.current));
Though using GameObject.Find is expensive better to store reference via start()
, or some other way...
You may just want to use the first selected ins$$anonymous$$d??
EventSystem.current.firstSelectedGameObject = someGameObject;
@AyA$$anonymous$$rau That's really great thanks. The lack of documentation for this is a bit annoying at the moment.
as of b19 you can just use:
eventSystem.SetSelectedGameObject(gameObject);
Your answer
Follow this Question
Related Questions
Unity 4.6 ui scroll rect won't show up in fullscreen 1 Answer
Unity5 UI - How to trigger button click event while preventing menu item deselect event? 1 Answer
Reliable way to get width/height of UI element? 3 Answers
Unity 4.6 GUI - Stretch/scale while rotated 2 Answers
HELP how to use buttons 1 Answer