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.
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
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
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.
@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.