OnMouseUp not working for clicking on GameObject

Hey all, I’m trying to click on an object with my mouse and run some script (to change what controllers are active - have that part figured out). I’ve searched and read through about 20 similar forum threads and I’m not sure what I’m doing wrong. I’d prefer to use an event rather than casting a ray during Update(). Help please :). I have a button in my scene with a sphere collider (trigger unchecked) and a script named ButtonFlyShip. The ButtonFlyShip script includes the code:

void OnMouseUpAsButton()
{
        Debug.Log ("Entered OnMouseUpAsButton");
}

I’ve also tried simply using OnMouseUp(), but I never enter that event either. I’m running the game through the Unity play button, and trying to click on the object using the center of the screen and/or my actual Windows mouse icon. Ideas? Thanks!

Oh, also, the Scripting Section FAQ has a link to this sort of thing, but the link is broken :(. http://forum.unity3d.com/threads/scripting-section-faq.19302/ - Detecting an object in 3D space under a point on the screen

try this: Unity - Scripting API: MonoBehaviour.OnMouseEnter()

Thanks. That didn’t work either, but on a whim, I added the ButtonFlyShip to a really large item in the world and tried clicking on that. It worked! So now I’m curious about the mouse aim and where it’s clicking. I did this:

void Start () {
        camera = GameObject.Find("PlayerCamera");
}
void Update () {
        Debug.DrawRay(camera.transform.position, camera.transform.forward * 50, Color.red);
}

I don’t see a ray. Perhaps I don’t see it because it’s a line with no width and I can’t see it from the end? How can I debug visually where exactly any mouse click ray will hit?

Hey, Jeddak. I have been looking around trying to solve this problem. I experience the same issue, and many other Unity users do. Just have a look at this interesting thread: OnMouseDown doesn't work with Sprite - Questions & Answers - Unity Discussions

Many say this issue is solved by changing the projection of the main camera from Orthographic to Perspective.

Many others say it will be solved by using a BoxCollider (and thus allowing for a depth value) instead of a BoxCollider2D.

If you get to solve this problem let us know.

With respect to your latest post: try using Debug.DrawLine instead, you may see your not-seeing-the-line problem solved. Let me know

Thanks Skinner:

  • Camera perspective: I was already running in Perspective
  • I was using a SphereColider.
  • I tried using Debug.DrawLine, and I still can’t see my line.

I did finally get to enter OnMouseUpAsButton() when I wanted. Again, on a whim, I looked up the hotkey for Playing my game through the Unity program (Ctrl+p), centered my mouse in the play window, and started playing - I could get the mouse event to fire for my large object, but not my intended button no matter what. Compared the colliders and they looked the same, so I just deleted and recreated the collider on my button and it works now. Also of note is I’ve determined that the actual Window’s mouse icon denotes where your in-game mouse is, not the center of my camera. I’m unsure how that works when I build and execute a full-screen exe of my game, but I’ll cross that road later I suppose.

Sigh…and I’m suddenly not entering OnMouseUpAsButton anymore. Thought I had it, but it’s acting like it’s a fickle method.