I have a group of prefabs distributed in a scene. Each prefab is made by some cubes and a floor (cubes are one color, floor is another). Each prefab has a collider attached to. I need to highlight the entire prefab when the mouse is over them. By “highlight” I mean temporarily changing to a slightly brighter color and when the mouse exits, the prefab returns to its original color. I’m using this code as a test:
private Color prevcolor;
void OnMouseEnter () {
prevcolor = gameObject.GetComponent<Renderer>().material.color;
gameObject.GetComponent<Renderer>().material.color = Color.red;
}
void OnMouseExit () {
gameObject.GetComponent<Renderer>().material.color = prevcolor;
}
The code above sent me this error: “MissingComponentException: There is no ‘Renderer’ attached to the “Pieza14” game object, but a script is trying to access it.
You probably need to add a Renderer to the game object “Pieza14”. Or your script needs to check if the component is attached before using it.”
When I substitute with GetComponentInChildren, only one of the cubes (presumably the first cube in the prefab) turns red and then returns to its original color. Excuse me because I’m relatively newbie in Unity and I’m missing something important but I can’t find out. Thanks in advance.