- Home /
Can't get highlight to work when mousing over object..
I have a grid of objects instantiated through a grid-making script. The script is attached to an empty gameobject in the scene.
Attached to each grid object (which appear as clones for some reason) is a script called "Highlight" which I'm trying to get to highlight each grid object green as I mouse over them. All that happens is the objects turn pink when instantiated and that's all. When I hover over them nothing happens either. I can assign the correct material to "normalColor" in the inspector which gets rid of the pink, but how do I get it to do this automatically? And how do I get my highlight working? Thanks! Total noob here.
using UnityEngine;
using System.Collections;
public class Highlight : MonoBehaviour {
public Renderer rend;
public Material normalColor;
public Color highlightColor;
void Start () {
rend = this.GetComponent<Renderer>();
rend.material = normalColor;
}
void Update () {
Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
RaycastHit hitInfo;
if (Physics.Raycast (ray, out hitInfo, Mathf.Infinity)) {
rend.material.color = highlightColor;
Debug.Log ("moused over object");
}
else {
rend.material = normalColor;
}
}
// void OnMouseOver () {
// // Debug.Log (gameObject.name + " OnMouseOver");
// Debug.Log ("moused over object");
// rend.material.color = highlightColor;
// }
//
// void OnMouseExit () {
// // Debug.Log (gameObject.name + " OnMouseOver");
//
// rend.material.color = normalColor;
// }
}
alt text
Yep, Thank you! That totally solved the mouse over issue. Now I just have the pink material when not mousing over problem and an incorrect material when I am mousing over.. I'm pretty sure it's just a $$anonymous$$or scripting mistake.
Here's exactly what's happening:
If I drag a TrackJump$$anonymous$$edium prefab into the editor it's material "betterdirt1" looks correct. Variables in the inspector read:
Rend "None (Renderer)" Normal Color "None ($$anonymous$$aterial)" Highlight Color green that I want, now semi-transparent
Then I hit play, the "betterdirt1" material disappears from inspector, and my variables read:
Rend "TrackJump$$anonymous$$edium ($$anonymous$$esh Renderer)" Normal Color "None ($$anonymous$$aterial)" Highlight Color green that I want, now semi-transparent
When I mouse over the now-pink prefab in the scene it indeed turns green but doesn't show the material underneath (I have changed Highlight Color to mostly transparent green ins$$anonymous$$d of opaque like it was in the above images). When I mouse off it returns to pink. I'm trying to get the prefab to display with "betterdirt1" material all of the time, with a green highlight when moused over.
It's pink because you didn't assign "betterdirt1" to "Normal Color" in the Inspector
Sometimes I wonder about myself lol... Thank you. That solved everything.