Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 Jun 22
sparklines
Close Help
  • 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
2
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 On$$anonymous$$ouseDown method to capture the event of the click.

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

You could use buttons ins$$anonymous$$d, 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 this 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
17

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 (this.gameObject.name + " Was Clicked.");
     }
 }

Alternatively, this can also be handled in the Inspector by adding an Event Trigger component.

Comment
Add comment · Show 1 · 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 On$$anonymous$$ouseDown() function on the script that wasn't working before you added the Event Trigger component.

avatar image
2

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

,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

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.

Follow this Question

Answers Answers and Comments

9 People are following this question.

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

how to make my game for all screen please ? 1 Answer

Changing screen resolution is cutting the very edge of my screen off 1 Answer

Rotate a camera when my flashlight is on the edge of the screen... Video included 2 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