- Home /
How can i highlight objects with the mouse when the mouse cursor is moving over objects ?
I have a Spacestation and under it as child a _Level and under it some more children. All the children under _Level are objects without mesh renderer or any renderer.
I added also a new Camera called it Map Camera and attached to it a script name Map. What i want to do is when i move the mouse cursor over any part(GameObject) of the _Level children it will highlight this object for example color it in white and when i move the mouse away it will color it back to it's original color.
Like a map in game that when you move the mouse over it will highlight the parts in the map.
This screenshot showing one of the childs of _Level and it's inspector. On the left the top window is the scene view the bottom is the game view. The screenshot is while the game is running.
For example in the screenshot the child name Corridor_Window_Part_05 So when the game is running in the game view window when the player move the mouse cursor over Corridor_Window_Part_05 then Corridor_Window_Part_05 should be highlight if the player is moving the mouse cursor to another child then the other child will highlight.
The second screenshot showing the Map Camera inspector.
This is the script that attached to Map Camera: The part with the M key displaying the map is working fine. The problem is how to make the highlight with the OnMouseEnter and OnMouseExit
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Map : MonoBehaviour
{
public Camera mapCamera;
private Camera[] cameras;
private Color startcolor;
// Use this for initialization
void Start()
{
cameras = Camera.allCameras;
}
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown(KeyCode.M))
{
if (mapCamera.enabled == false)
{
foreach (Camera cam in cameras)
{
cam.enabled = false;
}
mapCamera.enabled = true;
}
else
{
foreach (Camera cam in cameras)
{
cam.enabled = true;
}
mapCamera.enabled = false;
}
}
}
void OnMouseEnter()
{
GameObject levels = GameObject.Find("_Level");
foreach(Transform child in levels.transform)
{
child.gameObject.AddComponent<Renderer>();
child.GetComponent<Renderer>().material.color = Color.white;
}
}
void OnMouseExit()
{
}
}
I see now that it's not even getting to the On$$anonymous$$ouseEnter event. It's not firing. And i added to the $$anonymous$$ap Camera a collider. Box Collider. But still when running the game it's not getting to the On$$anonymous$$ouseEnter.
I used a break point.
You need some kind of a Collider on an object for the On$$anonymous$$ouseEnter() to fire
Answer by Harinezumi · Jan 18, 2018 at 10:41 AM
I would go about it a different way, a bit more object oriented, a bit more Unity component style. What this means is that instead of controlling from one central Map script, create a Highlightable component, and change material colors from there. For example:
public class Highlightable : MonoBehaviour {
[SerializeField] private Color highlightColor = Color.white;
[SerializeField] private Renderer ownRenderer = null;
private Color[] originalColors;
private void Start() {
if (ownRenderer == null) { ownRenderer = GetComponent<Renderer>(); }
StoreOriginalColor();
}
private void StoreOriginalColor () {
if (ownRenderer != null) {
Material[] materials = ownRenderer.materials;
originalColors = new Color[materials.Length];
for (int i = 0; i < materials.Length; ++i) { originalColors[i] = materials[i].color; }
}
}
private void OnMouseEnter() {
if (ownRenderer != null) {
Material[] materials = ownRenderer.materials;
for (int i = 0; i < materials.Length; ++i) { materials[i].color = highlightColor; }
ownRenderer.materials = materials;
}
}
private void OnMouseExit() {
if (ownRenderer != null) {
Material[] materials = ownRenderer.materials;
for (int i = 0; i < materials.Length; ++i) { materials[i].color = originalColors[i]; }
ownRenderer.materials = materials;
}
}
}
Watch out, this will only work for one renderer, and you can only set one color. Also, there might be some issue when assigning the materials back to the renderer, you might need to make a copy of the materials before reassigning.