How do I change the color of a Trail Renderer by script?

In a game that I’m making, I have bullets with a Trail Renderer attached, so there’s a white trail behind the bullet. What I’m trying to achieve is make it so the trail of the bullet turns orange when you use a power-up.

function Update()
{
	if(RatePowerup.powerActive == true)
	{
		//Change color of the bullet trail to orange
	} else 
	{
		//Change color back to white
	}
}

This script is attached to the Bullet object. “powerActive” is a static variable in the script attached to the power-up. Can anyone help? Thanks!

It is not true that the trail color cannot be changed. See here for a simple solution.

You have to give the trail object a material with shader Particles/Additive and then you can access the material’s tint color in code:

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

It is not possible to change the color of the Trail Renderer.

Workaround is:

Put an empty child game object under your bullet named “OrangeTrail” and attach a trail renderer with orange color to it and make the trailer renderer component disabled, then:

        if (RatePowerup.powerActive == true)
        {
            // Disable white trailer
            GetComponent<TrailRenderer>().enabled = false;

            // Enable orange trailer
            transform.Find("OrangeTrail").GetComponent<TrailRenderer>().enabled = true;
        }
        else
        {
            // Enable white trailer
            GetComponent<TrailRenderer>().enabled = true;

            // Disable orange trailer
            transform.Find("OrangeTrail").GetComponent<TrailRenderer>().enabled = false;
        }

So just toggling between two trailers is the solution.

Or you just create public Color variables and assign then to the TrailRenderer.startColor or .endColor.