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];
}
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);
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.
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.
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);
}
}
}
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
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