0

I am having serious problems with the Unity function "OnMouseUp" (also tried with OnMouseDown and others)

The problem I am getting is that the function is not reacting to clicks, but is not consistent on when it works and when it doesn't. Sometimes closing unity and open it back again made it work and sometimes it doesn't.

The code is fairly simple:

void OnMouseUp()
{
    Debug.Log("Test");
}

This script is added to a gameObject with the following characteristics GameObject

Up to this point I would think that there is some problem on my end and that I am doing things wrong, but after a bit of testing I believe is more like a Unity Bug than me.

An example of this is that sometimes it would work and sometimes it wont (on the same coordinates in-game)

And another example is that if the gameObject prefab has the "Is triggered" selected when launching the game and right before clicking into the gameObject with the OnMouseUp script on it, I deselect it, It magically work. If then I change my prefab box "Is triggered" and launch the game with that, it won't work. In another hand, if I then select again the "Is triggered" while the game is running, it would work again.

My theory is that for some reason when changing the property "Is Triggered" while the game is running, somehow it refreshes it and allows the code to run.

I did some research online to see what I could be doing wrong, but couldn't find anything. If anybody knows what my problem is it would be highly appreciated, and if it's a bug, any other way to achieve the same results?

  • 2
    Could be a bug. Anyways, you should be using `OnPointerUp` by implementing `IPointerUpHandler`. Read #7 from the answer on the duplicated question. – Programmer Jun 18 '17 at 01:27
  • Thank you, I didn't know this topic was discussed before. I took a look at related questions but couldn't find anything similar. – Paco Martos Triguero Jun 18 '17 at 01:35
  • 1
    That's fine. It was also asked [here](https://stackoverflow.com/questions/44583542/what-shall-i-use-instead-of-onmouse-event-handlers) just yesterday. – Programmer Jun 18 '17 at 01:47
  • I am sorry for bothering, but I am not sure how to implement this code, So far what I did was implementing the code under #7 to my main camera, and add to the script I have on the gameObject I want to be able to click: IPointerDownHandler, as well as the public void function that corresponds to that. I get no compile errors, but when i click on the object nothing happens. Sorry if the answer is really obvious but my programming level is really low. – Paco Martos Triguero Jun 18 '17 at 01:52
  • 1
    See #7 from the duplicated answer. Since you are using `SpriteRenderer` which is 2D, you must attach`Physics2DRaycaster` to the camera for that to work. Let me know if there is a problem – Programmer Jun 18 '17 at 02:18
  • I attached that to the main camera, but it still doesn't work. I dont know how to attach pictures here so I give the gyazo links: https://gyazo.com/0e1383a328468afe3b2bb562bdf5e39a https://gyazo.com/638f9f8f0406ad5807e2181083a61d6b – Paco Martos Triguero Jun 18 '17 at 02:27
  • 1
    The EnemyClickManager seem fine and should be attached to the sprite you want to detect click on. This sprite must have the a BoxCollider2D or any 2D collider. If this does not work please resize the collider and see what happens. – Programmer Jun 18 '17 at 02:31
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/146978/discussion-between-paco-martos-triguero-and-programmer). – Paco Martos Triguero Jun 18 '17 at 02:35

0 Answers0