user4530102
user4530102

Reputation:

Unity OnMouseEnter not working

I'll post the link to the project here I seem to be unable to figure out how to make my OnMouseEnter function work (it has worked in a previous project but won't now).

Google Drive - Space Cadets

using UnityEngine;
using System.Collections;

public class MouseHover : MonoBehaviour {

void Start(){
    GetComponent<Renderer>().material.color = Color.black;
}

void OnMouseEnter(){
    GetComponent<Renderer>().material.color = Color.white;
}

void OnMouseExit() {
    GetComponent<Renderer>().material.color = Color.black;
}
}

this code is attached to 5 menu items on the main menu which is supposed to make the text color go from black to white when the mouse hovers over it and I know that the first part of the script works but the OnMouseEnter is the part where it seems to not recognize (my thought is that the box collider that is supposed to recognize the mouse event isn't recognizing it).

Upvotes: 0

Views: 3149

Answers (1)

Jayson Ash
Jayson Ash

Reputation: 709

The Background gameobject in the scene overlaps the menu items. Change the position of the Background gameobject to be slightly behind the menu items and OnMouseEnter will register. Also, the line of code that changes the color on the material inside OnMouseEnter doesn't actually change the color of the text because the Font Material Text Color is set to black, when it should to be set to white while the TextMesh Color attribute is the one that should be toggled like so:

void Start()
{
    GetComponent<TextMesh>().color = Color.black;
}

void OnMouseEnter()
{
    GetComponent<TextMesh>().color = Color.white;
}

void OnMouseExit()
{
    GetComponent<TextMesh>().color = Color.black;
}

That's how I got it to work.

Upvotes: 1

Related Questions