- Home /
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);
}
Answer by CHPedersen · Dec 01, 2013 at 08:33 PM
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.
Ok, thanks. I'm not really familiar with shaders, any code example to setting it correctly (white)?
I have updated my answer with some additional code that should help you out.
Thanks, very informative. BTW, simply using the following command also made it white:
renderer.material.color = Color.white;
But it had to come in conjunction with:
line.SetColors(Color.white, Color.white);
Any one of those commands by itself would not set it white (the first command alone would make it gray...?)
I wonder what made them choose pink as the default material color of the line renderer ;)
Pink is generally chosen for a placeholder in graphics applications precisely because it is a very bright and noticeable color that isn't used as often as other colors. That way it is noticeable to developers if someone forgot to set a color for something.
That's interesting.
I think it's because accessing renderer.material causes it to automatically generate and set some default material which is probably the standard diffuse shader. Unless sharply lit by some nearby light, this shader renders a white material gray because it's only lit by the ambient lighting, which is gray. So it's like viewing a white object in an almost totally dark room = gray.
I do not know why line.SetColors afterwards would override that and make it purely white, though. But good testing. :)
I should note that my ambient light is pure white, so it's probably not a lighting issue.
Answer by skaiwate · Dec 29, 2013 at 02:05 AM
Should n't
line.SetPosition(0, Vector3.up);
be
line.SetPosition(1, Vector3.up);
?
Yes, it should. Copy-paste mistake. Edited question. This has nothing to do with the pink thing though.
Answer by AlirezaRastgoo · Nov 15, 2021 at 05:10 PM
It worked for me
Color Pink; ColorUtility.TryParseHtmlString("#FF8D2D", out Pink); LineRender.startColor = Pink; LineRender.endColor = Pink;
Your answer
Follow this Question
Related Questions
How can I create a glowing, smooth line of any shape? 2 Answers
Materials Compatible with LineRenderer 1 Answer
Changing LineRenderer color over time in update 1 Answer
Creating a fade out effect on a line renderer 0 Answers
How to fill with translucent color the space between two lines in Unity3d? 1 Answer