Search Unity

Simplest Script (OnMouseDown) Wont Work!!!

Discussion in 'Editor & General Support' started by Rodrigofrb, Oct 22, 2016.

  1. Rodrigofrb

    Rodrigofrb

    Joined:
    Oct 22, 2016
    Posts:
    1
    Hi guys,
    I'm having a hard time getting the simplest code to work.
    I'm trying to detect the click on a 2d GUI element (image). I have tried absolutely everything I saw on videos and tutorials, but it just does not work.

    As a step-by-step, I did the following:
    1- New Scene
    2- Add Canvas
    3- Add Panel to that Canvas
    4- Add Image To that Panel
    5- New C# script (refer to code below)
    6- Added the script to the image.
    7- Play & click the image.

    When I click the image, nothing happens!! Why??? Why God, Why????? (Sorry for the drama!)

    I have also tried adding a collider to that image, but it has had no effect.

    Thanks in advance for any insights.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class TestClick : MonoBehaviour {
    5.  
    6.     private void OnMouseDown()
    7.     {
    8.         Debug.Log ("BAZINGA");
    9.     }
    10. }
     
  2. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    8,281
    OnMouseDown requires the object to have a collider. For UI try adding a button and using the OnClick event or implement one of the interfaces into your class such as EventSystems.IPointerDownHandler.