• 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
1
Question by Mithrilshadow · Dec 12, 2013 at 06:04 AM · sprite

Detect when the mouse is over a sprite 2D.

I am looking for a simple way to detect which sprite in under the mouse when I mouse over it. OnMouseEnter() works with 3D physics but I couldn't get it to work with 2D physics. So I tried doing a Physics2D ray cast but couldn't get that to work. Any ideas?

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
4
Best Answer

Answer by Eric5h5 · Dec 12, 2013 at 06:06 AM

OnMouseEnter works fine with 2D physics but it has the same requirements as a 3D object, namely that the sprite has a collider.

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 Mithrilshadow · Dec 12, 2013 at 07:22 AM 0
Share

$$anonymous$$y test script is literally just: void On$$anonymous$$ouseEnter() { Debug.Log("I am over something"); } but it doesn't do anything when I mouse over any sprite with a collider.

avatar image Eric5h5 · Dec 12, 2013 at 07:28 AM 0
Share

Works fine here. Try starting with a completely empty project, add one sprite with that script, and see what happens. If it works, something else is interfering in your previous test.

avatar image Mithrilshadow · Dec 15, 2013 at 02:24 AM 0
Share

I did as you suggested, and started form a clean project and started adding things back in one by one. After getting all of the pieces in place its working fine. I think some was corrupt or got changed in the first project. Thanks for the advice!

avatar image mujaffars · Mar 20, 2016 at 09:34 AM 0
Share

works fine after adding collider :)

avatar image
-1

Answer by TheAngrycrow · Dec 14, 2013 at 03:09 PM

You need a collider component as well as a rigidbody. Most physics components in Unity don't work without a rigidbody.

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 Eric5h5 · Dec 14, 2013 at 06:17 PM 1
Share

You don't need a rigidbody, only a collider. The only reason for using a rigidbody is if you want the object to be under control of physics.

avatar image Mithrilshadow · Dec 15, 2013 at 02:22 AM 0
Share

Angrycrow is right you don't need a rigidbody. I tested it.

avatar image
0

Answer by Mai Kel · Feb 11, 2014 at 05:39 PM

I tried this and worked out. Somehow the Collider 2D area was not fitting my sprite. Try enlarging your collider size and check out.

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

20 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

Related Questions

Issue with PixelPerfectCamera script and sprite masks (see images)... How to fix? 1 Answer

make 2d sprite Follow Player With Fixed z position 0 Answers

Transparency Sort Axis on Y with Pivot instead of Center of object? 1 Answer

How to enforce specific sorting on Sprites 0 Answers

Use sprite mesh for filled image 1 Answer


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