Search Unity

Question Changing colors with material.SetColor(propertyName, color) vs. material.color

Discussion in 'General Graphics' started by Agreafel, May 5, 2023.

  1. Agreafel

    Agreafel

    Joined:
    Nov 10, 2017
    Posts:
    2
    So I am wrestling to get the intended, most common behavior, of material.color from using materal.SetColor("_Color", colorInput). I need to use SetColor() bc there are shaders that do not have the [MainColor] property set, and I don't believe I should need to use [MainColor] (.color) to change a color if I need or want to change specific colored properties. In the current use case I am trying to change a series of materials from a series of renderers, given a set of properties that are specified via the editor (for now).

    Code (CSharp):
    1.  
    2. private MeshRenderer[] meshRenderers;
    3. public List<string> customColors;
    4.  
    5. public void Start()
    6.     {
    7.         // get all mesh renderers in children
    8.         meshRenderers = GetComponentsInChildren<MeshRenderer>();
    9.         customColors.Add("_Color");
    10.         foreach (MeshRenderer meshRenderer in meshRenderers)
    11.         {
    12.             foreach (Material material in meshRenderer.materials)
    13.             {
    14.                 foreach (string customColor in customColors)
    15.                 {
    16.                     if (material.HasColor(customColor))
    17.                     {
    18.                         //Set default colors to revert changes, not explicitly the issue
    19.                     }
    20.                 }
    21.             }
    22.          
    23.         }
    24.     }
    25.  
    26.     public void ChangeColor(Color color)
    27.     {
    28.         // change color of all materials
    29.         foreach (MeshRenderer meshRenderer in meshRenderers)
    30.         {
    31.             foreach (Material material in meshRenderer.materials)
    32.             {
    33.                 foreach (string customColor in customColors)
    34.                 {
    35.                     if (material.HasColor(customColor))
    36.                     {
    37.                         material.SetColor(customColor, color);
    38.                     }
    39.                 }
    40.             }
    41.         }
    42.     }
    43.  
    And I am certainly passing standard Unity materials that would contain the "_Color" property, as if I use the material.color = color in place of the SetColor() method, it would work as expected.

    The primary issue is that the colors do not change during runtime while using SetColor(), but do change when setting the color with the standard color property.

    Is there something I am missing in doing this task, I mean obviously something is amiss, but from all that I am reading the two actions should be equivalent.

    https://docs.unity3d.com/ScriptReference/Material.SetColor.html
     
    Last edited: May 5, 2023
  2. Agreafel

    Agreafel

    Joined:
    Nov 10, 2017
    Posts:
    2
    I have found that the Standard URP Lit shader has changed the color property to be named "_BaseColor". This is not very well reflected in the documentation I found and provided. This made for debugging to be rather head racking.
     
    ikruze and abarcacrespom like this.