- Home /
Changing Color of Tree Material
SO what I'm trying to do is Put some trees in my scene. I'm using a default tree from a Unity Package, the tree called Sycamore (With Collider). In the Inspector window I can change the color of the material for the leaves. What I want to do, is to figure out how to get the material for the leaves to animate, meaning as I play the game have the leaves start out green, go yellow, orange, red, then back again continuously. Does anyone have any idea how I can set that up? I'm pretty new to Unity and know next to nothing about scripting, though any suggestions would be greatly appreciated!
Answer by joedrigon · Sep 09, 2010 at 06:48 PM
Maybe check out the Unity reference for material below...
// Fade the color from red to green // back and forth over the defined duration var colorStart = Color.red; var colorEnd = Color.green; var duration = 1.0;
function Update () { var lerp = Mathf.PingPong (Time.time, duration) / duration; renderer.material.color = Color.Lerp (colorStart, colorEnd, lerp); }
Wow this really helped, thanks a lot! It works! Now I just have to figure out how to get it to affect the right part of the tree lol. It's effecting park of the trunk, but not all of it, and not affecting the leaves.
This should be posted as a comment to the answer, and not a separate answer.
If you've received a helpful answer, remember to upvote it (click the up arrow next to it), and if it solved your problem, tick the box below the up/down arrows to mark it as the correct answer. This helps to keep UnityAnswers easy to use, and the best answers near the top to assist other users with the same question!
How did you change the color of the leaves? I can only change the trunk color.
Answer by getyour411 · May 01, 2014 at 02:40 AM
This old post was recently updated so I looked at it too. The issue is that the Sycamore has two materials and the proposed solution only chooses the first (the trunk).
You just need to change this line
renderer.material.color = Color.Lerp (colorStart, colorEnd, lerp);
to this
renderer.materials[1].color = Color.Lerp (colorStart, colorEnd, lerp);
Since there is more than one material, just change it to reference to materials and add the [1] bit. I tested this on the Sycamore w/ Collider tree and the leaves changed.
Be aware that if you are building this functionality in the hopes of later moving it to your Terrain trees, that won't work. I posted something about that awhile back and the code is pretty bad but at least it shows an idea
http://answers.unity3d.com/questions/521056/access-terrain-tree-material-from-script.html
Your answer
![](https://koobas.hobune.stream/wayback/20220613072958im_/https://answers.unity.com/themes/thub/images/avi.jpg)