Trail Renderer Colors [C#]

I am creating a Trail Renderer that is modified in script (width, life, material, etc.) I was wondering if there is a way to change the colors array in script as well.

I’ve tried getting the game objects “colors” variable but it says that does not exist even though it is in blue.

This is what I tried:

for (int X = 0; X < 5; X++)
{
trailRenderer.colors[X] = cTrailingColor[X];
}

cTrailingColor being my array of colors.

The colors array isn’t accessible from the script API, unfortunately.

1 Like

(Necro post go!)

You can change the color of the trail by cheating and instead tinting the material. (I used a 1x1 palette of white to base the material on.)

// Generate a random color.
nColor = new Color(Random.value, Random.value, Random.value, Random.value);

// Find the child game object that contains the TrailRenderer.
GameObject sphere = transform.Find("Sphere").gameObject;

// Get the material list of the trail as per the scripting API.
Material trail = sphere.GetComponent<TrailRenderer>().material;

// Set the color of the material to tint the trail.
trail.SetColor("_Color", nColor);

Great, but what if you want to change the whole array of colors?

so how we then fade out the trail? (i am not talking about trailR.time property)

Is the Colors component still not accessible via API? What’s the reason for this? I need to dynamically assign thousands of trail renderers and colors 0-4 properties … changing the material color doesn’t “smear” correctly.

Please fix this asap, unity.

Would be great to access the color array through script!

+1 for being able to change the colors array!

+1

Need exposing trail renderer color property. Tinting via materials more draw calls. Unless of course each trail renderer creates a draw call hit regardless. In any case, no variation is feasible via shader.

Would be nicer if color array is exposed. it would save from all the work arounds.

This may be ancient, but I need this function as well.

You could always cheat and do something like this:

using UnityEngine;
using System.Collections;

namespace UnityEditor
{    public class trailtest : MonoBehaviour
    {
        private void Start()
        {
            TrailRenderer tr = GetComponent<TrailRenderer>();

            SerializedObject so = new SerializedObject(tr);
 
            so.FindProperty("m_Colors.m_Color[0]").colorValue=Color.red;
            so.FindProperty("m_Colors.m_Color[1]").colorValue = Color.green;
            so.FindProperty("m_Colors.m_Color[2]").colorValue = Color.blue;
            so.FindProperty("m_Colors.m_Color[3]").colorValue = Color.white;
            so.FindProperty("m_Colors.m_Color[4]").colorValue = Color.black;
            so.ApplyModifiedProperties();

            // To show all properies
           SerializedProperty it = so.GetIterator();

            while (it.Next(true))
                Debug.Log(it.propertyPath);
        }
    }
}
2 Likes

Hey there !

My solution was to create a public Material[ ] in wich i dragged and dropped different materials with different colors.
Then, changing my trail material from script changes its color, easily !

For anyone still wanting to see this implemented (myself included), there is currently a feedback post requesting this feature here…vote it up and maybe we’ll finally get it :smile:

Does this actually work?

SerializedObject comes from the UnityEditor library, which is not available when you build your game, so this solution only works in the editor.

See:
http://answers.unity3d.com/questions/1102306/problem-with-unity-editor-namesapce.html
http://answers.unity3d.com/questions/576746/build-error-the-type-or-namespace-name-unityeditor.html

Since this is a top result on google for color changing trails I thought I’d drop this link in here to help some people out. It’s a good alternative to unitys built in trail renderer and you can change and re-number the array of colors via script.
http://wiki.unity3d.com/index.php?title=OptimizedTrailRenderer

Perfect! This worked for me. Any one know why TrailRenderer don’t have this functionality by default?