A common problem in Unity - my OnMouseUpAsButton() is not being called, and I do not understand why. I have attached box collider 2d (though its size is set up during runtime, but by the time I click on it it has proper size and covers the whole button). My object is an UI element, created from prefab during runtime. Here you can see its inspector info:

enter image description here


using UnityEngine;
using System.Collections;

public class button_controller : MonoBehaviour {
void OnMouseUpAsButton()
    Debug.Log("Clicked event button");
        case action.NONE:

I tried going through other threads where people had similar problems but nothing seems to work. Any ideas what might be happening?

EDIT: I found out that if I add a new button , directly from the inspector to the canvas, whose collider is within the canvas all other buttons start working. Interestingly I cannot put this new button anywhere, it has to be at least a bit within the canvas

  • \$\begingroup\$ Whats up with your inspector? Under BoxCollider, your suppose to have "Size" under "Offset". \$\endgroup\$
    – Gnemlock
    Sep 23, 2016 at 5:38
  • \$\begingroup\$ @Gnemlock you are right i accidentally cut it out. The collider size is 177x31 \$\endgroup\$
    – Tribski
    Sep 23, 2016 at 14:57
  • \$\begingroup\$ just to rule it out, could you put a placeholder image on your button before playtesting, and limit your code to just what youve shown us? \$\endgroup\$
    – Gnemlock
    Sep 23, 2016 at 22:05
  • \$\begingroup\$ @Gnemlock it still doesn't work. But I found something strange. If I add a new button directly to canvas (not to the Event UI or any of its children), all buttons start working (even those that are inside Event UI). No idea what's going on? \$\endgroup\$
    – Tribski
    Sep 24, 2016 at 2:43
  • 1
    \$\begingroup\$ My guess is that you added an actual Button UI, not an Image UI, and it added additional components. You've only given us the inspector view of the text box, and a generic function that isnt calling to begin with, so its impossible to tell what that may have been. Perhaps your Canvas was not set up? Regardless, I can not replicate your problem with the environment and code you have provided (past the solution I provided that you already implemented), so I am afraid there is not much more I can check.. \$\endgroup\$
    – Gnemlock
    Sep 24, 2016 at 23:57

2 Answers 2


Attempting to recreate the above environment, I was quickly able to recreate the problem. This stems from one or two small problems. Primarily, you have not set your collider up for interface through the OnMouseUpAsButton() method.

Using OnMouseUpAsButton()

Lets have a look at the API documentation on using this method. One particular line stands out:

This function is called on Colliders marked as Trigger if and only if Physics.queriesHitTriggers is true.

Sure enough, you do not have your collider set as a trigger. Enabling "trigger" via the inspector will allow your collider to trigger the OnMouseUpAsButton() method.

Make sure your collider is actually there

The above solution did not immediately fix the problem on my end, but I sourced the other issue down to the physical size of my collider. Due to the particular nature of my problem, and the unexplained absence of the size variables on the example BoxCollider2D, I will make note of them now.

First and foremost, without an image on the object, you will still see an outline. I assumed this was the collider, as you can always see the outline. This assumption was wrong. When I deliberetly selected Edit Collider the collider appeared in an alternate colour, and was considerably smaller than the original outline. As it appears, my button was working, I just had to enlargen the collider to make the clickable space more available. Previously, size has also been a reflection of scale, with a value of 1 always completely filling the object. This was also an incorrect assumption, in this case, as I instead had to have a size that matched the exact size value of the object.

I would strongly suggest you perform the same check. If this does appear to be an issue, Edit Collider should also make gizmos available to drag out the size of the collider. Oddly enough, in the example provided above, the size variable is completely absent from the BoxCollider2D component. While my current version of Unity is a single version out of date, this is not something I have ever seen before, so would not advise you alter the size from the inspector.

  • \$\begingroup\$ Thank you for your help. That being said I made a mistake while stitching the image, box collider has size, and it's 177.5 x 31.4. Also, selecting the "Is Trigger" didn't help, that being said It appears we disagree on what the documentation says. the line "This function is called on Colliders marked as Trigger if and only if Physics.queriesHitTriggers is true." seems to mean that the function is called ONLY IF the collider is NOT set or IF collider is set and Physics.queries.HitTriggers == true. That being said I tried both and it didn't help. Something must be eating the event :/ \$\endgroup\$
    – Tribski
    Sep 23, 2016 at 14:23
  • \$\begingroup\$ The API section you quote doesn't say the function is NOT called on non-trigger colliders. It says only that, for trigger colliders, there is an ADDITIONAL criterion (Physics.queriesHitTriggers) required for the function to be called. \$\endgroup\$
    – DMGregory
    Sep 24, 2016 at 6:16
  • \$\begingroup\$ @DMGregory, it doesnt say it is not, but replicating the environment asker provides above, this was the only requirement to actually getting it to work. \$\endgroup\$
    – Gnemlock
    Sep 24, 2016 at 23:54

There are a few issues here: it's late and I need sleep but why are you using a box collider on a canvas element ? Totally unnecessary and probably causing problems. If I wasn't on my phone I'd be of more help, sorry.

All that's required is you drag and drop the go with your button code into the button event field (might have to add the field first by clicking + button) and then select the function from the drop down menus. The function must be public and take zero to one arguments.

It's that simple.

  • \$\begingroup\$ It sounds like your confusing Canvas for EventSystem. An EventSystem is an alternate to the described method asker is using, but it is not being used in the above code. It also takes a little more than just dropping in a script for it to actually work.. \$\endgroup\$
    – Gnemlock
    Sep 23, 2016 at 10:19
  • \$\begingroup\$ In that case all he needs is something like a raycast as well as enabling isTrigger in the collider. Done. \$\endgroup\$
    – mad.meesh
    Sep 23, 2016 at 13:12
  • \$\begingroup\$ Oh wait I just realized onmousebuttonup could be a unity callback ha! If that case then your answer about isTrigger and collider size is right.. my mistake.... but even simpler.. \$\endgroup\$
    – mad.meesh
    Sep 23, 2016 at 13:14

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .