OnMouseEnter and OnMouseExit for multiple materials
Hey guys! I am writing a script to highlight the object in the scene. The object has 3 materials. So, using the example from the documentation for OnMouseOver ( https://docs.unity3d.com/ScriptReference/MonoBehaviour.OnMouseOver.html ) I wrote the script, which has the following idea:
MeshRenderer gets all materials from the object and puts in a list. Then in the for-loop the color of each material is added to the list of the initial materials' colors. In OnMouseEnter method each initial material is assigned a new color. And in the OnMouseExit (i haven't completed it yet) the initial materials are to be assigned to the object. Here is the script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Linq;
public class Selection : MonoBehaviour
{
public List<Material> bldg_mats_init;
public List<Color> mat_color_init;
private Material mat;
Color colorSelection = Color.red;
MeshRenderer m_Renderer;
void Start()
{
m_Renderer = GetComponent<MeshRenderer>();
List<Material> bldg_mats_init = m_Renderer.materials.ToList();
Debug.Log("Numer of bldg_mats_init:" + bldg_mats_init.Count);
for (int i = 0; i < bldg_mats_init.Count; i++)
{
mat = bldg_mats_init[i];
mat_color_init.Add(mat.color);
Debug.Log("Stored colors:" + mat_color_init.Count);
}
Debug.Log("Number of init mat colors:" + mat_color_init.Count);
}
private void OnMouseEnter()
{
Debug.Log("Number of init materials (OME):" + bldg_mats_init.Count);
Debug.Log("Number of init colors (OME):" + mat_color_init);
for (int i=0; i < bldg_mats_init.Count; i++)
{
material_bldg = bldg_mats_init[i];
m_Renderer.material.color = colorSelection;
}
}
private void OnMouseExit()
{
for (int i = 0; i < bldg_mats_init.Count; i++)
{
}
}
}
And it doesn't work. So the Debug.Log in the OnMouseOver method doesn't return the right number of materials obtained in Start. What could be the problem?
Thank you in advance!
Your answer
![](https://koobas.hobune.stream/wayback/20220613055144im_/https://answers.unity.com/themes/thub/images/avi.jpg)