Search Unity

HDRP changing color at runtime

Discussion in 'High Definition Render Pipeline' started by TheCakeIsReal, Sep 3, 2019.

  1. TheCakeIsReal

    TheCakeIsReal

    Joined:
    Feb 10, 2018
    Posts:
    7
    Hi,
    I've got an issue at changing the color of a material at runtime. I use the following code to set the color of the material using the HDRP Lit shader.

    Code (CSharp):
    1. Renderer renderer = GetComponent<MeshRenderer>();
    2. renderer.material.SetColor("_BaseColor", new Color(0f, 0f, 0f));
    When I open the inspector during runtime, the value of the color had been changed on the material instance, but the object is not displayed accordingly (it is still displayed with the previous color). However, if I manually deactivate and reactivate the object, then the right color is displayed. Any idea on what I'm missing here ?
     
  2. Refeas

    Refeas

    Joined:
    Nov 8, 2016
    Posts:
    192
    It's probably because
    material
    returns a copy of the object's material. Try using
    sharedMaterial
    instead.
     
  3. barbelot

    barbelot

    Joined:
    Jul 18, 2017
    Posts:
    38
    I had a similar problem where after changing the texture of an HDRP material via script at runtime, the displayed material would not be updated until I modified any of its parameters via the inspector (even parameters not related to the textures I was assigning).
    Using sharedMaterial instead did not solve the problem.