Search code examples
unity-game-enginerenderer

Change line renderer alpha unity


Unity's line renderer component has got a material to attach, so that the line renderer uses it to show the line renderer in the scene accordingly:

enter image description here

The problem is that if you change this material color it works, also in runtime it can be changed in the editor itself. But not so with the alpha. It does not respond neither in the edit nor in the code. I tried:

Color targetColor = new Color(0,0,0,a);
_unityEngineLineRender.sharedMaterial.SetColor("Color", targetColor);

and:

Color targetColor = new Color(0,0,0,a);
_unityEngineLineRender.sharedMaterial.color = targetColor;

and it doesn't work. Can thisn simple thing be done? if so, how?

Thanks in advance


Solution

  • The shader for your material probably does not support alpha, that's why you're not getting any transparency.

    The workaround is to write your own shader or use other that's built into Unity.

    Create a new material for your line renderer and select shader "Particles/Standard Unlit", set "Rendering mode" to "Fade". Then use it on Line Renderer instead of the default material.

    You can access the color using:

    renderer.material.SetColor("_Color", new Color(1f, 1f, 1f, 0.3f));