Search Unity

  1. Check out the Unite LA keynote for updates on the Visual Effect Editor, the FPS Sample, ECS, Unity for Film and more! Watch it now!
    Dismiss Notice
  2. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  3. Improved Prefab workflow (includes Nested Prefabs!), 2D isometric Tilemap and more! Get the 2018.3 Beta now.
    Dismiss Notice
  4. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    Dismiss Notice
  5. Want to see the most recent patch releases? Take a peek at the patch release page.
    Dismiss Notice

Trail Renderer Colors [C#]

Discussion in 'Scripting' started by KWright, Jul 12, 2010.

  1. KWright

    KWright

    Joined:
    May 8, 2010
    Posts:
    9
    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:

    Code (csharp):
    1. for (int X = 0; X < 5; X++)
    2. {
    3. trailRenderer.colors[X] = cTrailingColor[X];
    4. }
    cTrailingColor being my array of colors.
     
  2. andeeeee

    andeeeee

    Joined:
    Jul 19, 2005
    Posts:
    8,768
    The colors array isn't accessible from the script API, unfortunately.
     
    Smaselll likes this.
  3. Reiv3r

    Reiv3r

    Joined:
    Jan 22, 2011
    Posts:
    31
    (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.)

    Code (csharp):
    1.  
    2. // Generate a random color.
    3. nColor = new Color(Random.value, Random.value, Random.value, Random.value);
    4.  
    5. // Find the child game object that contains the TrailRenderer.
    6. GameObject sphere = transform.Find("Sphere").gameObject;
    7.  
    8. // Get the material list of the trail as per the scripting API.
    9. Material trail = sphere.GetComponent<TrailRenderer>().material;
    10.  
    11. // Set the color of the material to tint the trail.
    12. trail.SetColor("_Color", nColor);
    13.  
     
  4. koblavi

    koblavi

    Joined:
    Oct 1, 2010
    Posts:
    42
    Great, but what if you want to change the whole array of colors?
     
  5. Genie

    Genie

    Joined:
    Sep 12, 2009
    Posts:
    23
    so how we then fade out the trail? (i am not talking about trailR.time property)
     
  6. Charles-Van-Norman

    Charles-Van-Norman

    Joined:
    Aug 10, 2010
    Posts:
    86
    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.
     
  7. Duskling

    Duskling

    Joined:
    Mar 15, 2011
    Posts:
    1,197
    Please fix this asap, unity.
     
  8. lyons1005

    lyons1005

    Joined:
    Nov 3, 2010
    Posts:
    8
    Would be great to access the color array through script!
     
  9. tobmuell

    tobmuell

    Joined:
    Nov 12, 2012
    Posts:
    4
    +1 for being able to change the colors array!
     
  10. runningbird

    runningbird

    Joined:
    Sep 3, 2009
    Posts:
    372
  11. hippocoder

    hippocoder

    Digital Ape Moderator

    Joined:
    Apr 11, 2010
    Posts:
    23,610
    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.
     
  12. Waqqas Meraj

    Waqqas Meraj

    Joined:
    Oct 3, 2013
    Posts:
    2
    Would be nicer if color array is exposed. it would save from all the work arounds.
     
  13. AxisRob

    AxisRob

    Joined:
    Aug 6, 2013
    Posts:
    33
    This may be ancient, but I need this function as well.
     
  14. PGJ

    PGJ

    Joined:
    Jan 21, 2014
    Posts:
    822
    You could always cheat and do something like this:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. namespace UnityEditor
    5. {    public class trailtest : MonoBehaviour
    6.     {
    7.         private void Start()
    8.         {
    9.             TrailRenderer tr = GetComponent<TrailRenderer>();
    10.  
    11.             SerializedObject so = new SerializedObject(tr);
    12.  
    13.             so.FindProperty("m_Colors.m_Color[0]").colorValue=Color.red;
    14.             so.FindProperty("m_Colors.m_Color[1]").colorValue = Color.green;
    15.             so.FindProperty("m_Colors.m_Color[2]").colorValue = Color.blue;
    16.             so.FindProperty("m_Colors.m_Color[3]").colorValue = Color.white;
    17.             so.FindProperty("m_Colors.m_Color[4]").colorValue = Color.black;
    18.             so.ApplyModifiedProperties();
    19.  
    20.             // To show all properies
    21.            SerializedProperty it = so.GetIterator();
    22.  
    23.             while (it.Next(true))
    24.                 Debug.Log(it.propertyPath);
    25.         }
    26.     }
    27. }
     
    Sandsten and sGlorz like this.
  15. Nahinho

    Nahinho

    Joined:
    Jan 21, 2014
    Posts:
    4
    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 !
     
  16. faraday964

    faraday964

    Joined:
    May 16, 2014
    Posts:
    1
    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 :D
     
  17. MattLebrao

    MattLebrao

    Joined:
    Nov 24, 2013
    Posts:
    2
    Does this actually work?
     
  18. AdenFlorian

    AdenFlorian

    Joined:
    Feb 15, 2013
    Posts:
    7
  19. QuinnWinters

    QuinnWinters

    Joined:
    Dec 31, 2013
    Posts:
    308
    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
     
  20. Sandsten

    Sandsten

    Joined:
    May 28, 2015
    Posts:
    4
    Perfect! This worked for me. Any one know why TrailRenderer don't have this functionality by default?