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
0
Question by Sharckan · Jun 28, 2017 at 11:15 PM · spritecollider2dcollider 2d

UI Image doesn't react to OnMouseDown()

When I use a 2D sprite the script works, when I use it on the UI Image it doesn't. Does anyone know why?

I think it is because the polygon collider 2d doesnt reconize the UI image, and it reconizes the sprite. Don't know why even though it's the same image.

And the reason why I use the canvas and UI Images its because I'm making a 2d android app.

Do you guys know a solution?

Comment
Add comment
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

3 Replies

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

Answer by Ashokkumar-M · Jun 29, 2017 at 06:02 AM

You can Add EventTrigger Component to the Image and link it to OnMouseDown() method in the inspector with PointDown Event. Also don't forget to add EventSystem object in hierarchy. @Sharckan

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 Sharckan · Jul 03, 2017 at 11:33 PM 0
Share

I couldn't use On$$anonymous$$ouseDown() because I'm making an Apk, what I did was changing all the IU Images to 2D sprites. Anyway, I hope your answer helps someone. Thx

avatar image OusedGames · Jul 04, 2017 at 02:00 AM 0
Share

Check if there's an Event System in your scene, otherwise Inputs will not work.
- When you create a Canvas it automatically creates an gameobject called Event System that will handle interaction

avatar image
0

Answer by icecold043 · Mar 28, 2019 at 07:19 PM

Hey everyone i know it's been a while but i am asking a question. The event trigger will not work for me because I am making a game where you can control two player and i don't think the event trigger can switch to another gameobject when Im hitting for exemple "Left Ctrl". So is there anyway i can do this ? I made a script before but the OnMouseDown doesn't work on a UI object, sooo, help me please ^^ Here is my script :

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class ArrowLeft : MonoBehaviour
 {
     [SerializeField] GameObject[] PlayerT;
     public GameObject PlayerG;
     public float speed = 6f;
     Rigidbody2D rb;
     bool Player1Playing = true;
 
     public void Start()
     {
         PlayerG = PlayerT[1];
     }
 
     public void Update()
     {
         if (Player1Playing == true)
         {
             PlayerG = PlayerT[0];
         }
         else if (Player1Playing == false)
         {
             PlayerG = PlayerT[1];
         }

     if (Input.GetKeyDown(KeyCode.LeftControl))
     {
         Player1Playing = !Player1Playing;
     }
 
        //This was a test/ if (Input.GetKey(KeyCode.T))
         {
             // This was a test/ PlayerG.transform.Translate(Vector2.left * speed * Time.deltaTime);
         }
     }
 
     public void OnPointerDown()
     {
         PlayerG.transform.Translate(Vector2.left * speed * Time.deltaTime);
     }
 
 }
 
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
0

Answer by sarveshsvn · Mar 09, 2020 at 11:51 AM

I was facing the same issue. You need to implement interface,IPointerDownHandler.

StackOverflow - How to detect click/touch events on UI and GameObjects

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

92 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Refreshing the Polygon Collider (2D) upon sprite change? 1 Answer

Arrays? ↓↓↓↓↓↓↓ 2 Answers

Sprites and level tiles overlapping when they shouldn't 0 Answers

Click/Touch on Different part of sprite detected ? 0 Answers

2D Box Collider doesn't flip with Sprite 3 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