Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Problem with OnMouseDown & OnMouseOver in UI

Discussion in 'UGUI & TextMesh Pro' started by AppPhil, May 15, 2015.

  1. AppPhil

    AppPhil

    Joined:
    May 15, 2015
    Posts:
    5
    Hey,

    I created a cube in my 3D World in Unity (5). When I add a script, with one of the two functions OnMouseDown and OnMouseOver the function is called when I click the cube or go over it with the mouse. The problem is that, when I create a raw image in my canvas and add the same script, the functions are not called when I click.Please help me. Thanks in advance and sorry for my bad english.
     
  2. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    8,413
    The OnMouseDown and OnMouseOver functions do not work in the UI system. The EventTrigger system will give you what you need.
     
    AppPhil likes this.
  3. AppPhil

    AppPhil

    Joined:
    May 15, 2015
    Posts:
    5
    Okay Thanks! With the EventTrigger I can call a function, when I just go over the image. But how can I make it, that the function is called again and again while the mouse is over the image. With MouseEnter it only gets called once.
     
  4. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    8,413
    See this post where I posted some code to do this.
     
    AppPhil likes this.
  5. AppPhil

    AppPhil

    Joined:
    May 15, 2015
    Posts:
    5
    I don't know how to do it :D
     
  6. AppPhil

    AppPhil

    Joined:
    May 15, 2015
    Posts:
    5
    I got it. I have the methods OnPointerDown and OnPointerUp where I change the bool clicked to true and to false. And in update method I move the player while clicked is true.
     
    karl_jones likes this.
  7. UNITY3D_TEAM

    UNITY3D_TEAM

    Joined:
    Apr 23, 2012
    Posts:
    720
    yes correct!.finally u got it
     
  8. xarlybyte

    xarlybyte

    Joined:
    Apr 27, 2018
    Posts:
    4
    you need a object whit a Event System component. Graphiic raycaster added to your canvas and raycast target enabled on your ui element you wanna the click
     
  9. KevinCastejon

    KevinCastejon

    Joined:
    Aug 10, 2021
    Posts:
    110
    Just a necro to report that the code comments for OnMouseXXX callbacks says that it's working for Colliders ... and GUIElements (which has been removed since Unity 2019.3), it's confusing as one could think that it's working on UI, but it's not.
    Maybe it's time to clarify it directly into the code comment.