1
\$\begingroup\$

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>();
        btn.onClick.AddListener(TriggerFoo);
    }

    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:

enter image description here

enter image description here

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

\$\endgroup\$
1
\$\begingroup\$

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.

The <code>InputManager</code>, with the four required axis registered.

\$\endgroup\$
1
  • \$\begingroup\$ 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! \$\endgroup\$
    – Uknowho
    Aug 21 '16 at 12:25
-1
\$\begingroup\$

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.

\$\endgroup\$
7
  • \$\begingroup\$ this makes some sense, but I tried your suggestion and still nothing on console... \$\endgroup\$
    – Uknowho
    Aug 19 '16 at 22:45
  • \$\begingroup\$ 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. \$\endgroup\$ Aug 19 '16 at 22:51
  • \$\begingroup\$ Everything else is printing correctly to the console. The script should be able to do something basic as Debug.Log.... \$\endgroup\$
    – Uknowho
    Aug 19 '16 at 22:55
  • \$\begingroup\$ 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 \$\endgroup\$ Aug 19 '16 at 22:59
  • \$\begingroup\$ Have you gotten it fixed, @Uknowho? \$\endgroup\$ Aug 19 '16 at 23:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.