Search Unity

OnMouseDown doesn't work on a button

Discussion in 'Scripting' started by xydroh, Apr 9, 2015.

  1. xydroh

    xydroh

    Joined:
    Mar 7, 2015
    Posts:
    3
    Hello I tried starting up unity and add this simple script to a button but it just doesn't seem to work.
    I tried adding the script to buttons, panel's but the console just does nothing.

    using UnityEngine;
    using System.Collections;

    public class Click : MonoBehaviour {
    void OnMouseDown() {
    print ("click");
    }
    }

    this is a simple canvas with a panel and a button on It, there's just so little I can do wrong that I don't know why it won't work
     
    Last edited: Apr 9, 2015
  2. farhanblu

    farhanblu

    Joined:
    Dec 29, 2014
    Posts:
    49
    OnMouseDown() is used to detect tap on objects in your scene. Although Button is also an object, it is different in the sense that Unity gives you functionality to attach it's tap listener built-in and I guess OnMouseDown is not supposed to work on UI components (although I feel that if you attach a collider to your button, OnMouseDown might also work, but I haven't tried it). Just drag-drop a gameobject from hierarchy onto the button's On Click() in inspector, and choose a public method that you want called on button click.
     
  3. xydroh

    xydroh

    Joined:
    Mar 7, 2015
    Posts:
    3
    And what if I want to write the method myself I want to use on that button?
     
  4. farhanblu

    farhanblu

    Joined:
    Dec 29, 2014
    Posts:
    49
    One way is what I've described in my previous post. The other is to get reference of the button, then register it's event handler at runtime. It would look something like this :
    Code (csharp):
    1. Button a;
    2.  
    3. void Start()
    4. {
    5.     a.onClick.AddListener(() => {Debug.Log("Button a was clicked");});
    6. }
     
    tkddnr9546, kspanim8 and ptr0x like this.
  5. Philipp_Programmer_Germany

    Philipp_Programmer_Germany

    Joined:
    Apr 8, 2015
    Posts:
    9
    If you want if any Button is pressed the code can look like this! You can request it like this in the Update function i hope it helps you a little! And of course sorry for my bad english..
    Code (CSharp):
    1. public void Update()
    2.     {
    3.         if (Input.anyKeyDown)
    4.         {
    5.             Debug.Log("Any Key is pressed!");
    6.             // And here you can call any function if y want
    7.             // anyFunction(ParaType paramName,...);
    8.         }
    9.     }
     
    Westland likes this.
  6. Simonxzx

    Simonxzx

    Joined:
    Feb 22, 2015
    Posts:
    129
    The best way to do everything you want with a button is to use the new onClick implemented function; so create a new UI button and a script like the one below. Than attach it to a GameObject, like the main camera. Once you've done it click on the button you've created in the Hierarchy and drag with the mouse the Main camera from the Hiearchy to the box below the OnClick() sentence. You'll see another box above will appear, so select from it the script you've created (i named it "Try") and, from it, the public void you've created(that i named "TryTheClick()"). You have to create a PUBLIC void to allow the system interact with it
    So play start and try to click your button.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Try : MonoBehaviour {
    5.  
    6.     public void TryTheClick()
    7.     {
    8.         Debug.Log("Clicked !");
    9.     }
    10.  
    11. }
    12.  
     
  7. Westland

    Westland

    Joined:
    Jan 26, 2015
    Posts:
    27
    That seems like so much work for a rather simple thing to do.. But maybe it's me not understanding everything about it. I've looked at a few other examples, trying to get my button's onClick to work too, but everyone seems to suggest, what I feel are, complicated solutions. Oh well
     
  8. iamadityx

    iamadityx

    Joined:
    Aug 24, 2020
    Posts:
    1
    i have used this way and it works. but I don't know how to hack if the button is released. When the button is pressed i want the bool to be true but how do i make it false when it is released? please help!
     
  9. Unity_3D_Alex

    Unity_3D_Alex

    Unity Technologies

    Joined:
    Nov 11, 2020
    Posts:
    10
    Hi there,

    This should work, just attach to your button:

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.EventSystems;
    3.  
    4. public class MyButtonScript : MonoBehaviour, IPointerDownHandler, IPointerUpHandler
    5. {
    6.     public void OnPointerDown(PointerEventData data)
    7.     {
    8.         //Do the thing when button pressed
    9.         Debug.Log("Button pressed!");
    10.     }
    11.  
    12.     public void OnPointerUp(PointerEventData data)
    13.     {
    14.         //Do the thing when button released
    15.         Debug.Log("Button released!");
    16.     }
    17. }
    Hope that helps!
     
    synthc, BarriaKarl, 81neart and 7 others like this.