how to change the color of trail renderer?

…in script

js or C#?
just get the component and then you can acces the color.

public Color myColor;
void Start ()
{
myColor = gameObject.GetComponent<TrailRenderer>().renderer.material.color = myColor;
}

something like this…

It won’t work that way (I’ve tried). You’ll need to set the Tint color via SetColor. Something like:

myTrail.material.SetColor("_TintColor", newColor);

Your trail requires a material with a color on it btw. The color array for trail renderer is inaccessible.

thank you 4 answering my question,although it doesnt work.

tks a lot.it is perfect

Can you explain what you mean by “it doesn’t work”? I am using that one line to change the color of my trail renderers. How are you using that line of code? Does Unity throw any error? Or does it simply do nothing? If it’s not doing anything with no errors, can you post the codes that you’ve written?

EDIT
Okay now you say it works. Make up your mind! =(

I don’t know if it’s because Unity has changed over the years, but in Unity 5.3, you set the _EmissionColor if you’re using the standard shader

myTrail.material.SetColor("_EmissionColor", newColor);

I have a Trail Renderer with a Color and a Material made with URP/ParticlesUnlit shader.
None of the above work and _BaseColor for URP doesn’t work even after I Instantiate the sharedMaterial

var trail = selectedChild.GetComponent<TrailRenderer>();
trailMat = Instantiate(trail.sharedMaterial);
trail.material = trailMat;
// none of the following works even when trailMat = trail.material;
trailMat.color = newColor;
trailMat.SetColor("_BaseColor", newColor);
trailMat.SetColor("_EmissionColor", newColor);