melbel
melbel

Reputation: 25

Shader Change during runtime

I am trying to change shader on a trigger event during run time. However, when I used Debug.Log to check if all is right or wrong and everything runs except the shader does not change. There is something I missed ? Any help could be appreciated. Thanks very much

public void shaderChange() 
{
    rend = gameObject.GetComponent<Renderer>();
    shader1 = Shader.Find("Standard");
    shader2 = Shader.Find("Shader2");

    isActiveClip = !isActiveClip;
    if(isActiveClip == true)
    {
        rend.material.shader = shader2;
    }
    else
    {
        rend.material.shader = shader1;
    } 
}

Upvotes: 0

Views: 571

Answers (1)

Andrei Nego
Andrei Nego

Reputation: 59

The proper way to manipulate shaders is to change the assigned materials on the object. This means instancing a material for each use case and switching materials at runtime. This is because the material itself is dependent on the shader when being created.

Upvotes: 2

Related Questions