Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

OnMouseUp not working for clicking on GameObject

Discussion in 'Scripting' started by Palimon, Aug 6, 2014.

  1. Palimon

    Palimon

    Joined:
    Apr 18, 2013
    Posts:
    225
    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:
    Code (CSharp):
    1. void OnMouseUpAsButton()
    2. {
    3.         Debug.Log ("Entered OnMouseUpAsButton");
    4. }
    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!
     
  2. Palimon

    Palimon

    Joined:
    Apr 18, 2013
    Posts:
    225
  3. ar0nax

    ar0nax

    Joined:
    May 26, 2011
    Posts:
    485
  4. Palimon

    Palimon

    Joined:
    Apr 18, 2013
    Posts:
    225
    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:
    Code (CSharp):
    1. void Start () {
    2.         camera = GameObject.Find("PlayerCamera");
    3. }
    4. void Update () {
    5.         Debug.DrawRay(camera.transform.position, camera.transform.forward * 50, Color.red);
    6. }
    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?
     
  5. skinner92

    skinner92

    Joined:
    Feb 23, 2014
    Posts:
    112
    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: http://answers.unity3d.com/questions/587473/onmousedown-doesnt-work-with-sprite.html

    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.
     
  6. skinner92

    skinner92

    Joined:
    Feb 23, 2014
    Posts:
    112
    With respect to your latest post: try using Debug.DrawLine instead, you may see your not-seeing-the-line problem solved. Let me know
     
  7. Palimon

    Palimon

    Joined:
    Apr 18, 2013
    Posts:
    225
    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.
     
  8. Palimon

    Palimon

    Joined:
    Apr 18, 2013
    Posts:
    225
    Sigh...and I'm suddenly not entering OnMouseUpAsButton anymore. Thought I had it, but it's acting like it's a fickle method.