Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question OnMouseEnter and OnMouseExit for multiple materials

Discussion in 'Scripting' started by usernotauser, Apr 22, 2022.

  1. usernotauser

    usernotauser

    Joined:
    Nov 6, 2021
    Posts:
    12
    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:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using System.Linq;
    5. public class Selection : MonoBehaviour
    6. {
    7.      public List<Material> bldg_mats_init;
    8.      public List<Color> mat_color_init;
    9.      private Material mat;
    10.      Color colorSelection = Color.red;
    11.      MeshRenderer m_Renderer;
    12.    
    13.      void Start()
    14.      {
    15.          m_Renderer = GetComponent<MeshRenderer>();
    16.          List<Material> bldg_mats_init = m_Renderer.materials.ToList();
    17.        
    18.          Debug.Log("Numer of bldg_mats_init:" + bldg_mats_init.Count);
    19.          for (int i = 0; i < bldg_mats_init.Count; i++)
    20.          {
    21.              mat = bldg_mats_init[i];
    22.              mat_color_init.Add(mat.color);
    23.              Debug.Log("Stored colors:" + mat_color_init.Count);      
    24.          }
    25.          Debug.Log("Number of init mat colors:" + mat_color_init.Count);
    26.      }
    27.    
    28.       private void OnMouseEnter()
    29.      {
    30.          Debug.Log("Number of init materials (OME):" + bldg_mats_init.Count);
    31.          Debug.Log("Number of init colors (OME):" + mat_color_init);
    32.          for (int i=0; i < bldg_mats_init.Count; i++)
    33.          {
    34.              material_bldg = bldg_mats_init[i];
    35.              m_Renderer.material.color = colorSelection;
    36.          }
    37.      }
    38.      private void OnMouseExit()
    39.      {
    40.          for (int i = 0; i < bldg_mats_init.Count; i++)
    41.          {
    42.          }
    43.      }
    44. }
    And it doesn't work. The Debug.Log in the OnMouseOver method doesn't return the right number of materials obtained in Start. What could be the problem?
    I am also not sure how to pass the variables (lists) between methods.

    Thank you in advance!
     
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,186
    Let's take a look!
    At the top of your script, you have this.

    Code (CSharp):
    1.  public List<Material> bldg_mats_init;
    Then, within Start you have this

    Code (CSharp):
    1. List<Material> bldg_mats_init = m_Renderer.materials.ToList();
    Now, normally you'd think, these are one and the same! But nope, in this case these are two different variables. c# will try to anticipate what you want and within Start it will use the local variable named bldg_mats_init and not the one at the top of your script, because it just makes sense.

    If you want to store that info at the top, you'll need to not declare it within Start, but instead, just do the assignment.