Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 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 /
  • Help Room /
avatar image
0
Question by Tespy · Jan 01, 2017 at 11:20 PM · spriterenderercolor changeonmouseenteronmouseexit

OnMouseEnter causes an object to disappear for some reason

I was trying to use OnMouseEnter and OnMouseExit to make a sprite change color when you have your mouse on or off it. OnMouseExit works as it should, but OnMouseEnter makes the object disappear instead of changing the color. Though when I hardcode the sprite to change to red in OnMouseEnter, it works fine. Here is the relevant part of the script:

 public void OnMouseEnter()
     {
         //SprRender is a ref to the SpriteRenderer component
         SprRender.color = HighlightColor; //<- this somehow makes the sprite disappear
         
         //SprRender.color = Color.red; // <- this works as it should
         
     }

The skybox background is white, and the highlight color is blue, so I know it's not the highlight color blending in with the background. Also, if it helps any, I'm using Unity 5.0.0f4.

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

1 Reply

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

Answer by TBruce · Jan 01, 2017 at 11:20 PM

How are you defining your HighlightColor?

The problem is that the alpha value of HighlightColor is set to 0.

If you are doing it something like this

 public Color HighlightColor = new Color(0, 0, 0, 0);

then you are setting your transparency to 0. If you want to make the HighlightColor white you need to do it like this

 public Color HighlightColor = new Color(0, 0, 0, 1);

or this

 public Color HighlightColor = Color.white;

See the following Unity page for predefined color values

Comment
Add comment · Show 3 · 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 Tespy · Jan 02, 2017 at 07:20 PM 0
Share

I defined it as just

 public color HighlightColor;

I did that so that I could change it in the editor during runtime, rather than wanting to just choose any specific color (I just decided to go with blue for testing in runtime). Though after defining Highlight color as Color.black in the Start function (based on what you just said), it all started working. I could then change the color during runtime through the editor, and the sprite would change color as it should.

So apparently, all I had to do was explicitly define Highlightcolor as a nontransparent color, ins$$anonymous$$d of just leaving it declared. Is this supposed to happen? I mean, I thought that I could just declare the variable and the editor would handle the rest, but apparently not xD

avatar image RobAnthem · Jan 02, 2017 at 07:39 PM 0
Share

Yeah you are basically just assigning a null color to the object texture, making it appear to vanish. TBruce is right, if you aren't setting this color prior to your mouseover event, then set a default value in the declaration.

avatar image Tespy · Jan 02, 2017 at 10:18 PM 0
Share

@RobAnthem: Oh, I see. Also, after a bit more messing around, I found I can set a color in the editor before running the game, and have it show up if I add this to the Start function:

 HighlightColor.a = 1f;

So, that's an alternative to setting it to some other random color I may not want.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

OnMouseEnter OnMouseExit, and Event Triggers Not Working? 1 Answer

What is the code for making the player died only when he collide with a platform that have a different color as him? 0 Answers

Javascript 2D change color of spriteRenderer through script 1 Answer

How to ignore specified color when changing sprite color? 0 Answers

Sprite not properly updating color 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