Search Unity

Cannot Set Property on MeshRenderers

Discussion in 'Shaders' started by nttLIVE, Mar 15, 2019.

  1. nttLIVE

    nttLIVE

    Joined:
    Sep 13, 2018
    Posts:
    80
    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class Test : MonoBehaviour
    4. {
    5.     public MeshRenderer meshRenderer;
    6.  
    7.     void Start()
    8.     {
    9.         Debug.Log("Triggered");
    10.         meshRenderer.material.SetColor("Color", Color.red);
    11.     }
    12. }
    I've created a shader using Shader Graph and I cannot access any of the properties. The property is exposed. I'm using Shader Graph, HDRP and Hybrid Rendering. I tried SkinnedMeshRenderer, accessing different property types, different names, using SetPropertyBlock and nothing's working. I can create a new RenderMesh component in ECS with a new material that has modified properties fine, but I cannot change properties in real time, at least not through the MeshRenderer.

    Is this a bug?

    Using Unity 2018.3.7f1 if it matters.
     
    Last edited: Mar 15, 2019
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,343
    No, the property you're changing is unlikely to be named "Color". That's how it might appear in the Shader Graph, but that's just it's display text, not the actual property name.

    In your Shader Graph, select your color property and expand it. You'll see a "reference" value with something like "_Color_A5GUZ02ETC", that's the actual property name you need to use when setting the value from script. You can also try to change it to something more sane, like "_MyColor" if you wish.
     
    nttLIVE likes this.