Search code examples
c#unity-game-engineshader

I cannot change alpha value of material. Error in shader


My code to change alpha value:

Color color = other.gameObject.GetComponent<Renderer>().material.color;
        color = new Color(color.r, color.g, color.b, 0.5f);

        other.gameObject.GetComponent<Renderer>().material.color = color;

Error I get when trying to change alpha value:

My Shader File(MultipleUVBlend01):

https://codecollab.io/@proj/OfferChapterBasket#


Solution

  • The color in you shader is set to a custom name "_MainColor" so you need to use GetColor() and SetColor().

    Color color = this.gameObject.GetComponent<Renderer>().material.GetColor("_MainColor");
            color = new Color(color.r, color.g, color.b, 0.5f);
            this.gameObject.GetComponent<Renderer>().material.SetColor("_MainColor", color);