Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Can't set color for LineRenderer, always comes out as magenta or black

Discussion in 'Scripting' started by gmfbrown, Sep 11, 2020.

Thread Status:
Not open for further replies.
  1. gmfbrown

    gmfbrown

    Joined:
    Jan 9, 2019
    Posts:
    25
    I am trying to create a line renderer and set its color in a C# script. By default line renderers are always magenta. I found three different ways in the Unity documentation to set the color of a line renderer. One of them, LineRenderer.setColors, is apparently obsolete. The other two methods are as follows:

    lineRenderer.startColor = Color.gray;
    lineRenderer.endColor = Color.gray;

    When I do this the line renderer still comes out as magenta. I've tried using other colors than Color.gray and the same result happens. The other method I tried was as follows:

    lineRenderer.material.SetColor("_Color", Color.gray);

    This time, it does change the color, but it changes it to black instead of gray. Once again, I've tried using other colors than Color.gray and the same result happens.

    Does anybody know how you can write code to set the color of a line renderer that will result in the line renderer being the color you set it for instead of some other arbitrary color?
     
  2. Zer0Cool

    Zer0Cool

    Joined:
    Oct 24, 2014
    Posts:
    203
    The color magenta means that the shader of the line renderer is not working or not set. Try to use another shader for the line renderer, therefore make a new material and assign it to the line renderer. Look at the examples of the line renderer to find the right one. As stated in the manuals the default material "Default-Line" should work fine for the line renderer.

    So the solution is:
    - use the "Default-Line" as material for the line renderer OR
    - make a new material, assign the shader "Legacy Shaders/Particles/Alpha Blended Premultiply" and assign this material to the line renderer

    For setting the colors by script, the current line renderer uses a color gradient. Here's a simple example for 2 colors from green to red:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. public class SetLineRendererColors : MonoBehaviour
    5. {
    6.     public Color startColor = Color.green;
    7.     public Color endColor = Color.red;
    8.  
    9.     private LineRenderer lr;
    10.  
    11.     void Start()
    12.     {
    13.         lr = GetComponent<LineRenderer>();
    14.         lr.material = new Material(Shader.Find("Legacy Shaders/Particles/Alpha Blended Premultiply"));
    15.         // Set some positions
    16.         Vector3[] positions = new Vector3[3];
    17.         positions[0] = new Vector3(-2.0f, -2.0f, 0.0f);
    18.         positions[1] = new Vector3(0.0f, 2.0f, 0.0f);
    19.         positions[2] = new Vector3(2.0f, -2.0f, 0.0f);
    20.         lr.positionCount = positions.Length;
    21.         lr.SetPositions(positions);
    22.         // A simple 2 color gradient with a fixed alpha of 1.0f.
    23.         float alpha = 1.0f;
    24.         Gradient gradient = new Gradient();
    25.         gradient.SetKeys(
    26.             new GradientColorKey[] { new GradientColorKey(startColor, 0.0f), new GradientColorKey(endColor, 1.0f) },
    27.             new GradientAlphaKey[] { new GradientAlphaKey(alpha, 0.0f), new GradientAlphaKey(alpha, 1.0f) }
    28.         );
    29.         lr.colorGradient = gradient;
    30.     }
    31. }
    You can use these 2 functions too, but setting the color gradient is more powerful:
    https://docs.unity3d.com/ScriptReference/LineRenderer-startColor.html
    https://docs.unity3d.com/ScriptReference/LineRenderer-endColor.html
     
    Last edited: Sep 11, 2020
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,756
    This essentially takes a copy out of the material renderer and changes the color, as per docs here:

    https://docs.unity3d.com/ScriptReference/Renderer-material.html

    You could bring it out to a local copy of a Material, change it, then assign it back to the .material property.

    Or else, perhaps try this instead:

    Code (csharp):
    1. lineRenderer.sharedMaterial.SetColor("_Color", Color.gray);
    from:

    https://docs.unity3d.com/ScriptReference/Renderer-sharedMaterial.html

    But just beware of the implications of your choice.
     
    richardkettlewell likes this.
Thread Status:
Not open for further replies.