Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
2
Question by recon472 · Aug 28, 2014 at 06:56 PM · uiunity 4.6selectionbeta

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

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
6
Best Answer

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

Comment
Add comment · Show 12 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image recon472 · Aug 28, 2014 at 08:32 PM 0
Share

awesome! thank you so much :)

avatar image Humph · Sep 05, 2014 at 09:52 AM 0
Share

This looks really useful. I am stuck now on what to pass in for the pointer argument?

avatar image AyAMrau · Sep 05, 2014 at 04:24 PM 1
Share

@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

avatar image CrandellWS AyAMrau · Nov 10, 2015 at 12:30 AM 0
Share

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;
avatar image Humph · Sep 05, 2014 at 11:31 PM 0
Share

@AyA$$anonymous$$rau That's really great thanks. The lack of documentation for this is a bit annoying at the moment.

avatar image recon472 · Sep 27, 2014 at 08:48 AM 1
Share

as of b19 you can just use:

 eventSystem.SetSelectedGameObject(gameObject);
Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges