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 () {
	
	}
}

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.

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.

87068-screen-shot-2017-01-31-at-175829.png
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

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.

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.

Really late to this thread, but there is another reason the method may not be visible. I had renamed the class name such that the class name no longer matched the script file name. When I changed the class name to match the script file name it showed up in the OnClick method select UI.

public void myButton(int q){ // “q” it is name select different any
Debug.Log(“Hello World” + q);
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Select you UI button, click inspector menu scroll down, On Click() click+ if no, select you Script or Script in object, open right menu select you name Script, select you public void in Sctipt.

Run the game in the editor, not in full screen (uncheck Maximize On Play button) Select the EventSystem GameObject of your Walgreenslistens scene. In the inspector, open the Preview Window and make it big enough. Move your mouse on your screen, and check if the name of your buttons appear next to the field pointerEnter.

You can use the built-in On Click event. This video shows step-by-step how to call a function from a script using the button on click event:

Another thing, the function apparently must have a return type of void.

public void chatButton()
{
    Debug.Log("yo");
}