Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by jaekx · Oct 07, 2015 at 08:55 PM · uibuttonscript.

New to the new UI (buttons)

I need help! I'm not used to the new Unity UI system, as far as I know you can only add individual scripts to buttons. However, in my situation I want one script to handle the functionality of many buttons attached to an empty game object or something. Heres an image of what I want to try and program currently:

alt text

For the current arrow buttons I want the word to change from skin -> hair - > eyes as pressed accordingly, how would I go about this with the new system? Make a global integer variable and use that in each button script??

unity-trouble.png (5.2 kB)
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

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by cjdev · Oct 08, 2015 at 09:11 AM

If you want to control your button elements from a centralized location you first need to get them with GameObject.Find or GameObject.FindGameObjectWithTag or something similar to get a reference to the GameObject containing the button. Then you get the button component from that object with GetComponent<Button>(). Even though you don't have access to the editor's onClick event menu you can still assign functions as the button's listener by using generic functions (lambda expressions). Basically it looks something like this:

 myButton.onClick.AddListener(() => MyFunction());

This will attach the handler that makes MyFunction run whenever the button is clicked. But the advantage of generic functions is that you can make the lambda expression contain the contents of the function itself. A lot of the time a button is only meant for a simple task, like switching a bool or setting a value, so it pays to have a way to add that functionality without a lot of extra mess in your code. To do it you'd just do:

 myButton.onClick.AddListener(() => { Debug.Log("Foo"); });

And then you have a way to add as much functionality as needed to a button from any external class without having to worry about needing a script for each and every button.

Comment
Add comment · 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
0

Answer by ItsaMeTuni · Oct 07, 2015 at 09:12 PM

Try:

 public void OnGUI(){
         if(GUI.Button(new Rect(10,70,20,30), "label")){
             //Your functionality code goes here
         }
     }

Where 10 and 70 are the location (X and Y) and 20 and 30 are the size (X and Y). Sorry if my text is a bit confusing.

Link to documentation: http://docs.unity3d.com/ScriptReference/GUI.Button.html

Comment
Add comment · Show 1 · 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 mmcelt · Oct 01, 2016 at 11:21 PM 0
Share

Wrong UI system..The question is about the new UI system.

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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Spawn prefab on UI button click 0 Answers

Best way to handle multiple buttons? (C#) 3 Answers

Move gameObject on UI Button Press 2 Answers

How do I change UI button background AND button text on click(pressed)? 0 Answers

Unity UI OnClick AddListener 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