LineRenderer drawing in pink?

Hi,

Very simple code to create and draw a line renderer, but for some reason the line is drawn in pink.

Any ideas?

public void Start() {
	GameObject lineObj = new GameObject("DragLine", typeof(LineRenderer));
	LineRenderer line = lineObj.GetComponent<LineRenderer>();
	line.SetWidth(0.04f, 0.04f);
	line.SetColors(Color.white, Color.white);
	line.SetPosition(0, Vector3.zero);
	line.SetPosition(1, Vector3.up);
}

Pink’s the default color for when the material is either missing, or there’s an error in the shader it’s using. In your case, I’m guessing the material’s missing. You have to specify a material for it to use. Depending on your exact situation, I find that particle shaders often work well for line renderers.

Update:

You don’t need to write any shader code to just make it pure white. That’s doable with just a standard unlit shader. If you’re creating this LineRenderer purely from script, you can make a material based on the built-in Unlit/Texture shader like this, and then add it to the LineRenderer’s renderer. This is based on your code:

public void Start()
{
    GameObject lineObj = new GameObject("DragLine", typeof(LineRenderer));
    LineRenderer line = lineObj.GetComponent<LineRenderer>();
    line.SetWidth(0.04f, 0.04f);
    line.SetColors(Color.white, Color.white);
    line.SetPosition(0, Vector3.zero);
    line.SetPosition(1, Vector3.up);
    Material whiteDiffuseMat = new Material(Shader.Find("Unlit/Texture"));
    line.material = whiteDiffuseMat;
}

Notice that while most shaders use the vertex colors, it must be unlit if you want it to be purely white. If not, you’ll get some sort of gray or grayscale gradient as lighting calculations vary the color across the line.

Important final bit of info:

Materials, like textures, are not cleaned up automatically. When you destroy objects whose scripts have explicitly created materials like this, you MUST manually destroy the material asset yourself. Forgetting to do so causes a memory leak. Assuming you have a script that gets attached to the line, you can add this to it:

private void OnDestroy()
{
    Destroy(this.renderer.material);
}

Doing so correctly cleans up after the created material. If the line does not have its own script (so nowhere to put above snippet), then you have to acquire a reference to the material yourself in another script and destroy it alongside the line.

Should n’t

line.SetPosition(0, Vector3.up);

be

line.SetPosition(1, Vector3.up);

?

It worked for me

Color Pink;
ColorUtility.TryParseHtmlString(“#FF8D2D”, out Pink);
LineRender.startColor = Pink;
LineRender.endColor = Pink;