- Home /
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 () {
}
}
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.
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!
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.
@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.
I've tried this its not working for me. I have tried Public, and static public.
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.
Answer by japhib · Jun 09, 2017 at 11:43 PM
I was having this problem and the following fixed it:
Create a new Empty game object in the scene.
Drag the script into the empty game object.
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.
Answer by mukundagarwal · Jan 31, 2017 at 06:21 PM
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
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.
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.
Your answer
![](https://koobas.hobune.stream/wayback/20220613192510im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Can I do recursion in unity3d compiler using javascript? 1 Answer
How do I allow my function to run only once at a time? 1 Answer
How is calling a function from a button click handled internally? 1 Answer
Button is unable to grab functions with two parameters? 0 Answers
Invoke other UnityEvent with gameobject.GetComponent().onClick.AddListener(); 2 Answers