# Unity - UI button not triggering custom script function

I have created simple movement buttons in my game. They should trigger functions of the player's scripts, but I can't link the button to the player object/script canonically from the inspector 'cause the player is created at runtime. So I created a custom script attached to the buttons:

using UnityEngine;
using UnityEngine.UI;
using System.Collections;

public class ActionButton : MonoBehaviour {

private Button btn;

void Start()
{
btn = GetComponent<Button>();
}

public void TriggerFoo()
{
Debug.Log("Btn triggered!!!!");
}
}


Haven't implemented yet the behavior I want to achieve with it, but even at this point the button doesn't print the string on console.

Here is how the button is set up in the inspector:

I am missing something very obvious here....?

### What your missing is an EventSystem object.

Your button is passing an OnClick event when you click on it; but without an EventSystem to manage your events, this can have seemingly unpredictable results. The EventSystem is its own component, and it does not appear to matter where you place it in your hierarchy, just as long as you only have one of them.

I personally had a very similar issue while testing an android prototype game I had made. It took me several months to figure it out, and even than, it wasn't me that figured it out - I also had to ask about it, here on GameDev. While someone was able to point out my initial issue through comments, I am still quite unclear on the overarching issue that had me confused; it appears (or at least, in my case) that input may work without an EventSystem in an actual build. Assume it won't, and ensure you always include one. I keep one on my main camera, and have been able to successfully test your code with it.

### If your using the CrossPlatformInput package, you also need to ensure you have matching axis in your InputManager.

Since you have not mentioned it, I assume you are not using the Unity package CrossPlatformInput. Just in case you are, buttons still won't work if you do not have corresponding axis set in your InputManager.

For CrossPlatformInput to work, it needs to interact with four separate axis in your InputManager: Vertical, Horizontal, Submit and Cancel. If you do not have these axis, fatal errors will block your UI from handling input. Simply having these axis does not suitably set them up for CrossPlatformInput, but having them there allows your other input to work as intended.

• Thanks @Timelord64, that was definitely it...! I didn't consider the Event System at all and I though one was automatically generated when creating a UI button. I have no idea why I had not one in my project. Anyway with this solution my buttons are now working. Thanks! Aug 21 '16 at 12:25

Click the + thing right under onclick. Now, choose the object "still" (the button). Now, simply find the script then function. In your setup, the button is only storing the script, and the button component isn't actually using it.

• this makes some sense, but I tried your suggestion and still nothing on console... Aug 19 '16 at 22:45
• That is exactly how you'd use a button set up. You need to try having it activate a sprite instead, since you can guerrantee whether it'll work. Also, with the console, click the text message box on the console bcause it may be trying to display it, but the console is hiding it. Aug 19 '16 at 22:51
• Everything else is printing correctly to the console. The script should be able to do something basic as Debug.Log.... Aug 19 '16 at 22:55
• Is the button calling the TriggerFoo function? Also, get rid of the code in Start. It's pointless. Also may be messing up the script Aug 19 '16 at 22:59
• Have you gotten it fixed, @Uknowho? Aug 19 '16 at 23:12