Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

how to change the color of trail renderer?

Discussion in 'Scripting' started by JohnSonLi, Apr 22, 2013.

  1. JohnSonLi

    JohnSonLi

    Joined:
    Apr 15, 2012
    Posts:
    586
    ...in script
     
  2. jister

    jister

    Joined:
    Oct 9, 2009
    Posts:
    1,749
    js or C#?
    just get the component and then you can acces the color.

    Code (csharp):
    1. public Color myColor;
    2. void Start ()
    3. {
    4. myColor = gameObject.GetComponent<TrailRenderer>().renderer.material.color = myColor;
    5. }
    something like this...
     
  3. wolfhunter777

    wolfhunter777

    Joined:
    Nov 3, 2011
    Posts:
    534
    It won't work that way (I've tried). You'll need to set the Tint color via SetColor. Something like:
    Code (csharp):
    1.  
    2. myTrail.material.SetColor("_TintColor", newColor);
    3.  
    Your trail requires a material with a color on it btw. The color array for trail renderer is inaccessible.
     
  4. JohnSonLi

    JohnSonLi

    Joined:
    Apr 15, 2012
    Posts:
    586
    thank you 4 answering my question,although it doesnt work.
     
  5. JohnSonLi

    JohnSonLi

    Joined:
    Apr 15, 2012
    Posts:
    586
    tks a lot.it is perfect
     
  6. wolfhunter777

    wolfhunter777

    Joined:
    Nov 3, 2011
    Posts:
    534
    Can you explain what you mean by "it doesn't work"? I am using that one line to change the color of my trail renderers. How are you using that line of code? Does Unity throw any error? Or does it simply do nothing? If it's not doing anything with no errors, can you post the codes that you've written?

    *EDIT*
    Okay now you say it works. Make up your mind! =(
     
  7. Lexeon

    Lexeon

    Joined:
    Apr 22, 2016
    Posts:
    23
    I don't know if it's because Unity has changed over the years, but in Unity 5.3, you set the _EmissionColor if you're using the standard shader

    Code (csharp):
    1.  
    2. myTrail.material.SetColor("_EmissionColor", newColor);
    3.  
     
  8. aahmedacc

    aahmedacc

    Joined:
    Jun 9, 2022
    Posts:
    1
    I have a Trail Renderer with a Color and a Material made with URP/ParticlesUnlit shader.
    None of the above work and _BaseColor for URP doesn't work even after I Instantiate the sharedMaterial

    Code (CSharp):
    1. var trail = selectedChild.GetComponent<TrailRenderer>();
    2. trailMat = Instantiate(trail.sharedMaterial);
    3. trail.material = trailMat;
    4. // none of the following works even when trailMat = trail.material;
    5. trailMat.color = newColor;
    6. trailMat.SetColor("_BaseColor", newColor);
    7. trailMat.SetColor("_EmissionColor", newColor);