Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
9
Question by Anthonylinx · Nov 13, 2013 at 07:53 PM · click4.3

Detecting Mouse Click on 2D Sprite

Hello, all I'm trying to do is detect if the sprite rectangle contains the mouse position however I have no idea how to get access to a Sprite gameObject's rectangle. I have the same effect working with GUIText but would like to make this work to make a menu using sprites as buttons.

I'd appreciate any help, thank you.

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

4 Replies

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

Answer by Eric5h5 · Nov 13, 2013 at 08:29 PM

The OnMouseDown/Over/Up functions work as usual. Like all objects except GUIElements, sprites need colliders for that to work.

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 Anthonylinx · Nov 13, 2013 at 08:32 PM 0
Share

Ah, thanks very much. I'm somewhat new to Unity and am still trying to understand all the basics.

avatar image
6

Answer by rosdi · Jun 01, 2014 at 09:10 AM

The only way for this to work, apart from making sure there is Collider component, is to make sure the Z index of the object is on top, even if you are making 2D games. The sorting layer is ignored and Z index take precedence instead.

So this means you may not be able to attach your sprite under another game object. I tried parenting my sprite under another game object and my mouse click is no longer detected.

Comment
Add comment · Show 4 · 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 nsxNawe · Jun 01, 2014 at 04:07 PM 0
Share

Rosdi, Im having this same problem, I tried removing the parent object but no luck... I didnt quite get the "Z index" you said, you mean that is must be the closest object to the camara in Z axis? like not having any object in between consu$$anonymous$$g the mouse events?

Cheers and thanks for your help!

avatar image rosdi · Jun 03, 2014 at 04:17 AM 0
Share

@nsxNawe, Yes.. to test this create a simple project with two sprites in the scene.. one sprite make its Z index 0 and another -1. Both game object must have Collider 2D component. Only the object with -1 Z index will capture the On$$anonymous$$ouseUp event.

avatar image Arunraj · Oct 16, 2015 at 10:24 AM 0
Share

$$anonymous$$any thanks @rosdi ! You made my day great !!! 2D mouse Click working after changing z values

avatar image nowchoi88 · Apr 23, 2016 at 04:26 AM 0
Share

Thank you. your answer is great!!

avatar image
3

Answer by pctroll · Nov 13, 2013 at 09:48 PM

Add a collider to the game object (preferably 2D) and then call the functions OnMouseDown/Over/Up as @Eric5h5 told you. I want to emphasize that these functions won't work unless you add a collider 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 CodyMoores · Mar 05, 2014 at 10:11 PM 0
Share

Should I tick is Trigger?

avatar image Anthonylinx · Mar 05, 2014 at 11:28 PM 0
Share

It does not have to be a trigger, it just needs a collider component attached. Hope that helps

avatar image
0

Answer by Romano · Feb 03, 2014 at 12:23 PM

I've just put a post on my game's blog about 2D mouse clicking in unity because I had such a hard time finding the answer anywhere. There's also a link to a javascript script that allows you to click on sprites taking into account their sorting layer and sorting order. Hope this helps: http://notquiteblackandwhite.com/post/75474540217/how-to-do-2d-mouse-clicks-in-unity

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

25 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

Related Questions

Using RayCast to Get Mouse Input 1 Answer

Help paint will not work!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! 2 Answers

Run script when object is clicked 1 Answer

Auto Primitive Collider 3 Answers

Camera Orbit on Left Click problem 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