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
0
Question by Andrew_Best · Nov 06, 2014 at 05:01 AM · mousemouseover

2D OnMouseOver issues

I'm currently trying to get two variations of OnMouseOver() to work, however no OnMouseXXX() function would work for me.

I was trying to use OnMouseDown() a while back which wouldn't work for me at all, I eventually got this to work:

 if (Input.GetMouseButtonDown(0))
 {
     Vector2 p = (Vector2)Camera.main.ScreenToWorldPoint(Input.mousePosition);
     Collider2D[] hits = Physics2D.OverlapPointAll(p);
     for (int i = 0; i < hits.Length; ++i)
     {
         //content
     }
 }

However now I'm coming back to trying to get OnMouseOver() to work as I need to play a selection sound when the user hovers over mouse elements. This is all that I've tried:

 void OnMouseOver()
 {
     selectSound_.Play();
 }

I had previously tried more but I erased all of that code a few weeks back when I managed to get the Input.GetMouseButtonDown(0) code working.

Does anyone have any ideas as to why OnMouseOver() doesn't work for me? Or perhaps an alternative?

Note: When using OnMouseOver() the function never fires, and I have both colliders and GUI elements.

Comment
Add comment · Show 4
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 robertbu · Nov 06, 2014 at 05:06 AM 0
Share

Note that because of the way you have this structured, you will call 'Play()' every frame while the mouse is over the object. You tested your problem with a Debug.Log()? Any chance there is another (invisible) collider between the camera and the object with the On$$anonymous$$ouseOver() script? Your script would work with another collider since it only test if a collider is there not the collider. And the On$$anonymous$$ouseOver must be on the same game object as the collider, and that collider must be front-most with respect to the camera.

avatar image Andrew_Best · Nov 06, 2014 at 03:22 PM 0
Share

I did all of my testing with Debug.Log() to no avail, and I do know play will be called every time, I plan to fix that so it'll only fire once.

I have checked for outside colliders that would interfere and there aren't any. When you say the On$$anonymous$$ouseOver() has to be on the same game object as the collider, what do you mean exactly? Do you mean I need to have an On$$anonymous$$ouseOver() attached to every object that I want to have something happen with? If this is the case, that could very well likely be my issue.

Edit: To add on to this however, I have tried all of the On$$anonymous$$ouseOver() On$$anonymous$$ouseEnter(), etc with GUI elements, which isn't working for me either.

avatar image robertbu · Nov 06, 2014 at 03:32 PM 0
Share

For the use of On$$anonymous$$ouse??? functions, the script must be on the object with the collider, so you will need a script attached to every object that you want to detect the On$$anonymous$$ouse??? behaviors. And unlike your Raycast() code, only the front-most collider will fire.

avatar image spiceboy9994 · Nov 07, 2014 at 07:01 PM 0
Share

What type of collider you have on your game object with the On$$anonymous$$ouseEnter() script attached? Is it 2d or 3d?

0 Replies

· Add your reply
  • Sort: 

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

How can i tell if the mouse is over the game window? 5 Answers

I need to be able to detect the mouse pointer entering a trigger area *through* an object that I'm dragging. 0 Answers

MouseOver different Objects C# 1 Answer

How do I detect when mouse passes over an object? 4 Answers

MouseOver issue with Collider on prefab 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