3

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?

1
  • Did you check the return value of Shader.Find()? Commented Jan 13 at 14:46

3 Answers 3

3

I got it working by modifying these lines as follows:


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

Sign up to request clarification or add additional context in comments.

1 Comment

Do mark the this answer accepted and close this question if you think this is the correct solution. :)
1

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.

Comments

0

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);

1 Comment

Thanks. I tried it, but still getting same error on shader: "Hidden/InternalShaderError".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.