Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 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
3
Question by Ian-McCleary · Apr 08, 2015 at 03:28 AM · buttonfunctioncalling

How do you call a function with a button? Unity 5 UI

I have two input field boxes that I would like the user of my application to fill-out with numbers. I have made a somewhat simple script to transform the imputed numbers into integers and then have them be multiplied. Lastly they are transformed back into a string.

I have named the function Product in my script and i would like for it to be called every time the user clicks a specific button. I want the answer to product to appear as a new text in the bottom of the screen.

So far i have an onclick script (it needs more) and i want to know how i can make it call the product function when the mouse is clicked even though the mouse click and product function are on two separate scripts and game objects.

Just an FYI....I have heard of the On value change function for input field boxes that should update it automatically but im unaware of how this works as well or how to implement it.

I attached the scripts below and if you need clarification feel free to ask!! Thanks in advance!

Calculation Script using UnityEngine; using UnityEngine.UI; using System.Collections; using System;

 public class Calculations : MonoBehaviour {
     
     public GameObject textField_1;
     public GameObject textField_2;
     public Text Result;
     InputField t1;
     InputField t2;
 
     void Start()
     {
         t1 = textField_1.GetComponent<InputField> ();
         t2 = textField_2.GetComponent<InputField> ();
     }
 
     
     public void Product() {
         int a = Convert.ToInt32(t1.text);
         int b = Convert.ToInt32(t2.text);
         int c = a*b;
         Result.text = c.ToString ();
     }
     
 }


Button click script

 using UnityEngine;
 using UnityEngine.UI;
 using System.Collections;
 using System;
 
 public class CalculateButton : MonoBehaviour {
 
 
     [SerializeField] private Button MyButton = null; // assign in the editor
     
     void Start() { MyButton.onClick.AddListener(() => { Product();});
     }
     
     // Update is called once per frame
     void Update () {
     
     }
 }
 
Comment
Add comment · Show 2
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 Nerevar · Apr 08, 2015 at 10:03 AM 2
Share

You should check the event trigger system, I don't know if you are aware of it but it could help you.

video tuto here.

avatar image Ian-McCleary · Apr 08, 2015 at 11:52 PM 0
Share

Hey thank you so much. I was unaware of this specific tutorial. I had seen other unity tutorials but could not find the one to help. This is so great! Thank you again!

8 Replies

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

Answer by Norfinity · Apr 09, 2015 at 04:14 AM

Select the button in your hierarchy, there should be a "Button" component on the inspector. Add a new entry (+ symbol) in the "On Click()"-List. Now you have to drag the GameObject with the attached script into the new entry field (no-object), after that you will be able to select a function which will be called everytime you clicking the button.

Comment
Add comment · Show 5 · 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 AK123 · Apr 15, 2015 at 07:59 PM 1
Share

the function that i created is not showing in the list

avatar image Scramblejams AK123 · Oct 26, 2015 at 05:53 PM 1
Share

@A$$anonymous$$123: $$anonymous$$ake sure the functions you wrote are declared to be public. I had to do that to ensure they appeared in the list.

avatar image TashaSkyUp Scramblejams · Jan 01, 2017 at 08:07 AM 1
Share

I've tried this its not working for me. I have tried Public, and static public.

Show more comments
avatar image HakJak · Jun 24, 2016 at 02:28 AM 1
Share

How can I do this via script?

I have a QualitySettings$$anonymous$$enu game object that is set to not destroy in load, and has a script that will destroy duplicates immediately. Problem is, when I reload the scene, I lose the connection to the button.

avatar image
9

Answer by japhib · Jun 09, 2017 at 11:43 PM

I was having this problem and the following fixed it:

  1. Create a new Empty game object in the scene.

  2. Drag the script into the empty game object.

  3. Drag this object, not the script, onto the "OnClick()" portion of the Button component of your button, as others have mentioned.

Only after attaching the script to an object first, and THEN attaching that object to the button, did the function show up.

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 Fourst4r · Apr 17, 2018 at 10:33 AM 1
Share

Thanks a lot, this should be the "Best Answer".

avatar image
2

Answer by mukundagarwal · Jan 31, 2017 at 06:21 PM

alt text I was also having the same issue. A colleague of mine helped out. My script was linked to the canvas. Instead of adding the script to the button, I added the canvas instead. And then I could access the script and the public function. Hope this helps.

Mukund


screen-shot-2017-01-31-at-175829.png (11.1 kB)
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
1

Answer by MarkD · Apr 09, 2015 at 12:22 AM

You can attach a Transform to the button and from the OnPress action list, you can select functions you created on the scripts that that Transform carries.

For example: Transform A has a script that contains the function called DestroyObject containing the code Destroy(A.gameObject).

drag this Transform in the button slot and select the name of the script"DestroyObject" then from that list select your function.

As soon as you press that button, object A will get destroyed.

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
1

Answer by FalseVacuumDev · Sep 20, 2017 at 07:07 AM

The custom function you want to use must have 1 or no parameters. If your custom function has no parameters and it still does not work add a string parameter (useless parameter), sometimes unity needs a parameter to detect a custom function for some reason.

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
  • 1
  • 2
  • ›

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

34 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 avatar image avatar image avatar image avatar image avatar image

Related Questions

Pressing space calls the wrong function 1 Answer

determine the minimum time for the player to hold the button 1 Answer

Run a function aslong as a button is pressed 3 Answers

How to set GUI button "1" to "0"? 1 Answer

How to add an IF statement based on a button click 3 Answers


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