Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
2 captures
11 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 juliocdep · Aug 03, 2015 at 04:21 PM · c#onmouseenteronmouseexit

OnMouseEnter when enabling a object

I have a game object with the following script for example:

 public class ExampleClass : MonoBehaviour {
     public Renderer rend;
     void Start() {
         rend = GetComponent<Renderer>();
     }
 
     void OnMouseEnter() {
         rend.material.color = Color.red;
     }
 
     void OnMouseExit() {
         rend.material.color = Color.white;
     }
 }

In other script I handle this game object disabling or enabling it. The problem is when I disable it and the mouse is away of the game object, if I enable it again and now the mouse is on the object, in this situation the OnMouseEnter event will never be called. It only will be called if I exit and enter again. How to solve that, I have to use OnMouseOver just to check this?

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Dave-Carlile · Aug 03, 2015 at 04:22 PM

You might be able to handle with this with OnEnable. You'd have to check to see if the mouse is over the object and set the color accordingly. Not much different than just using MouseOver, but only runs once instead of each frame.

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 juliocdep · Aug 04, 2015 at 12:16 PM 0
Share

I thought about that, but how to know if the mouse is over on the object in OnEnable?

avatar image
0

Answer by NeverHopeless · Aug 04, 2015 at 01:22 PM

Perhaps like this: (untested code to give you an idea, i assume it is 2D, if not you have to remove the 2D elsewhere)

 bool stateChanged = false;
 
 void Update()
 {
     if(stateChanged) {
         if(gameObject.isEnabled && Physics2D.OverlapPoint(cameraConvertedMousePos) != null)
         {
              // color.red
         } else {
              // color.white
         } 
         stateChanged = false;
     }
 }
 
 void OnEnable()
 {
     stateChanged = true;
 }
 
 
 void OnDisable()
 {
     stateChanged = true;
 }


Hope it helps!

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Multiple GUI textures one script 2 Answers

[Solved]MouseEnter not working! 1 Answer

Making a bubble level (not a game but work tool) 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