- Home /
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!
Answer by chaosmaker · Nov 08, 2013 at 01:03 AM
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.
I had to do some troubleshooting with this script, because some things produced a bunch of errors. For example, when you get components it should be just "GetComponent(TrailRenderer)". None of those angle brackets.
Thanks for the help, though! I'll mark this answer as correct anyways
Hey, @Nuitari, I wouldn't say it's wrong, it's just slightly over complicated. Your answer is great, but I already have this working, and it's been working for a while. It's a little bit late to change it...
Yeah it is right, you can have it tinted through material but it doesn't mean that you can access the color array of it (In case you want to have two unique trailers). See this post for more details:
http://forum.unity3d.com/threads/54162-Trail-Renderer-Colors-C
True, but the question wasn't about changing the color array, it was about making a white trail orange. Anyway, hope this helps other people with similar problems.
Answer by Nuitari · Nov 26, 2013 at 10:31 AM
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);
Answer by imuniz · Aug 01, 2019 at 03:03 PM
Or you just create public Color variables and assign then to the TrailRenderer.startColor or .endColor.
Your answer
![](https://koobas.hobune.stream/wayback/20220613121426im_/https://answers.unity.com/themes/thub/images/avi.jpg)