• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
3
Question by $$anonymous$$ · Aug 24, 2015 at 01:30 PM · c#screenui imageonmousedownapplication.quit

UI Image and OnMouseDown not working

I made an UI Image as button and I use a script for click:

 void Start() {
 }

 void Update() {
 }
 
 void OnMouseDown() {
     Application.Quit();
 }

UI Image has the script.

Comment
Add comment · Show 3
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Landern · Aug 24, 2015 at 01:32 PM 0
Share

Do you have a collider on the UI element? This is necessary for the OnMouseDown method to capture the event of the click.

avatar image JayFitz91 · Oct 12, 2015 at 07:51 AM 0
Share

You could use buttons instead, remove the text from the button and replace its default source image with the image you want and scale to the appropriate size. After that, you can simply use the default OnClick function built directly into the button to call the function you want

avatar image Willard720 · Aug 13, 2018 at 01:48 AM 0
Share

Please don't include empty functions, such as start and update.

3 Replies

· Add your reply
  • Sort: 
avatar image
5
Best Answer

Answer by Free Arm Studios · Oct 12, 2015 at 07:45 AM

I was literally just experiencing t$$anonymous$$s same problem. I found that if I put a Button component on it, that'll fix it right up.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
20

Answer by NateJC · Dec 20, 2016 at 04:38 AM

For UGUI UI objects, instead of OnMouseDown, use OnPointerDown:

 using UnityEngine;
 using System.Collections;
 using UnityEngine.UI;
 using UnityEngine.EventSystems; // Required when using Event data.
 
 public class ExampleClass : MonoBehaviour, IPointerDownHandler // required interface when using the OnPointerDown method.
 {
     public void OnPointerDown (PointerEventData eventData) 
     {
         Debug.Log (t$$anonymous$$s.gameObject.name + " Was Clicked.");
     }
 }

Alternatively, t$$anonymous$$s can also be handled in the Inspector by adding an Event Trigger component.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image lionelmarr · Jul 14, 2020 at 06:25 PM 0
Share

After two days struggling with the problem, I would just like to stress the ease and satisfaction of adding an Event Trigger component to the game object. (I had seen this answer but not acted on it because it didn't sink in).

To expand a bit: Add component > Event > Event Trigger; Add a new event (I added Pointer Click); Click on the + button, then drag the object into the GameObject Slot In my case, the same object as I was adding the Event Trigger component to); Click on the "No Function" field to choose a function that will be called on clicking, which will be the OnMouseDown() function on the script that wasn't working before you added the Event Trigger component.

avatar image yangjon1997 · Nov 26, 2022 at 10:36 AM 0
Share

Hi,

I want to ask why for UGUI UI objects we should use OnPointerDown instead or OnMouseDown? Asking because currently working on a project where OnMouseDown isnt working but OnPointerDown is.

Thanks !

avatar image
2

Answer by xarlybyte · Apr 06, 2020 at 10:06 AM

,you need a object w$$anonymous$$t a Event System component. Grap$$anonymous$$ic raycaster added to your canvas and raycast target enabled on your ui element you wanna the click

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

10 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

OnMouseDown for Right Mouse 3 Answers

Fixing the camera's left side in a 2D Game in all screen resolutions 0 Answers

How can I move a player toward a gameobject by clickling an image 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges