Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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
6
Question by Flickayy · Aug 24, 2014 at 11:06 PM · c#uibuttons4.6

[4.6 - UI] Calling function on button click via script

So I've been trying to mess around with the UI in 4.6/5.

I'm wondering how to call a function in a C# script when a UI Button has been clicked, like when trying to advanced some text been shown. I just need to find out how to check if the button has been clicked.

I have imported the UnityEngine.UI and UnityEngine.EventSystems, not sure how to use it though.

Comment
Add comment · Show 1
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 cregox · Dec 12, 2014 at 12:41 PM 0
Share

quite related: http://gamedev.stackexchange.com/questions/83027/unity-4-6-gui-dynamically-change-the-functions-called-by-gui-elements

2 Replies

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

Answer by AyAMrau · Aug 24, 2014 at 11:39 PM

There are two ways around it:

  • Add listeners in the editor - if your button is placed in the editor and always does the same thing, then this is easiest. Select the button and find the Button component. At the bottom, there is an On Click() field. There you can select and object, script component and function on that script (function must be public), you can also add values to pass.

  • Add listeners in script:

    [SerializeField] private Button MyButton = null; // assign in the editor

    void Start() { MyButton.onClick.AddListener(() => { MyFunction(); MyOtherFunction(); });
    }

Comment
Add comment · Show 15 · 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 Flickayy · Aug 24, 2014 at 11:49 PM 0
Share

The listener approach was my goal. Thanks for the helping hand!

avatar image Vanlandi · Aug 29, 2014 at 05:22 PM 0
Share

Thanks a lot for this. I'd like to point out that when you add the new function through script, it does NOT reflect in the editor, but it does work when you click the button.

avatar image Vanlandi · Aug 29, 2014 at 06:40 PM 1
Share

I have another question related to this. How does one assign a variable to the function call? Sort of in the same way you can assign a variable to the persistent listener. PS: I don't mean to highjack this thread, should I just ask a new question?

avatar image AyAMrau · Aug 29, 2014 at 07:08 PM 4
Share

you can pass variables to the functions you call in the listener as normal:

 $$anonymous$$yButton.onClick.AddListener(() => { $$anonymous$$yFunction("string literal"); $$anonymous$$yOtherFunction($$anonymous$$yButton.name); });

There is no variable send with the event that occurs OnClick, so all you can do is capture something from the existing objects or pass a specific value.

And yes, it would be better to make new questions if it's not strictly related to this one. I also answered quite a few UI things in last few days, so if you search there may already be an answer ;).

avatar image AyAMrau · Sep 16, 2014 at 12:11 AM 1
Share

@LordYabo You can just use normal delegates ins$$anonymous$$d.

But the lambdas* shouldn't really cause issues here, so if you want you can post your code and we can try to spot what the problem is.

*Just for sake of correctness, this is called a lambda expression, it's widely used with Linq, but in this context Linq has nothing to do with this ;).

Show more comments
avatar image
0

Answer by Aztec · Dec 04, 2014 at 01:14 PM

I was stuck at a similar problem recently. I had multiple buttons added via script and each of them should call a function with another parameter. There was a lot of trouble because everytime only the last button in the list called the function instead of the other one i really wanted to. So my trick was to add a short script (only with my listener and the variable) to each button Prefab. So if the buttons are created there is no Problem with the sharing of the listeners.

I hope i could save someone a little bit of time ^^

Comment
Add comment · Show 2 · 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 AyAMrau · Dec 04, 2014 at 02:08 PM 0
Share

There are other solutions to this issue, with an explanation as to why it happens, here:

http://answers.unity3d.com/questions/791573/46-ui-how-to-apply-onclick-handler-for-button-gene.html

It's good to understand and keep this issue in $$anonymous$$d, because with ui code the listeners are widely used, so this may come up quite often.

avatar image m_pangolin · Dec 24, 2014 at 12:13 PM 0
Share

You really helped me. thanks!

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

29 People are following this question.

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

Related Questions

Please help! After build on Android UI buttons can't enable/disable script but works when using Unity remote. 0 Answers

How can you make images appear on (UI) buttons in a scroll box/scrollable list/scrollview? 0 Answers

How convert this Door Script to work with Ui Buttons! 1 Answer

Double Jump With UI BUTTONS; 0 Answers

Creating working UI Button from Script? 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