3
votes

I am using the new HDRP/LitTesselation shader.

I would like to change the Base Color + Opacity at runtime:

img

I added this code to the game object's script:

void start()
{
        Color color = new Color(100, 50, 100, 150);

        //Fetch the Renderer from the GameObject
        Renderer rend = GetComponent<Renderer>();

        //Set the main Color of the Material to green
        rend.material.shader = Shader.Find("_Color");
        rend.material.SetColor("_Color", color);
}

But it generates an Hidden/InternalShaderError error in the shader. Can anyone point me in the right direction?

3

3 Answers

3
votes

I got it working by modifying these lines as follows:


rend.material.shader = Shader.Find("HDRenderPipeline/LitTessellation"); 
rend.material.SetColor("_BaseColor", color);

1
votes

This is for HDRP/Lit:

private Material _mat;

void Start()
{
    Renderer nRend = GetComponent<Renderer>();
    _mat = nRend.material;
}

void Update()
{
    Color nNew = //do whatever you want here
    _mat.SetColor("_BaseColor", nNew);
}

I once read that "sharedMaterial" should be used instead of "material". However, I think that was just a typo. "sharedMaterial" would affect ALL HDRP/Lit materials, I think.

0
votes

The issue is most likely with this line:

Color color = new Color(100, 50, 100, 150);

According to the Unity docs, colors should be initialized with values from 0 to 1 instead of with larger numbers. My guess is that if you change the value of your color variable accordingly that will solve the issue. The rest of your code seems to follow the form found here.

Try out the following:

Color color = new Color(0.39f, 0.196f, 0.39f, 0.588f);