- Home /
Changing leaf colors in C#
So, I have a few ideas for this but I don't know how to directly change the textures of a leaf on a tree using: using UnityEngine; using System.Collections;
public class SimpleTimer: MonoBehaviour {
public float targetTime = 60.0f;
Update(){
targetTime -= Time.deltaTime;
if (targetTime <= 0.0f) { timerEnded(); }
}
void timerEnded() { meshRenderer.material.SetTeture("_MainTex", anotherTexture"); }
}
How could i make this work?? Also, I have little to none experience in c# or unity in general. Thanks!
Answer by Bleakmountain50 · Oct 25, 2018 at 12:04 AM
if you give every leaf a tag called "Leaf" then this should work:
public float targetTime = 60.0f;
public Material leafColor;
void Update()
{
if (targetTime > 0)
{
targetTime -= Time.deltaTime;
}
if (targetTime <= 0.0f)
{
targetTime = 0.0f;
timerEnded();
}
}
void timerEnded()
{
var leaf = GameObject.FindGameObjectWithTag("Leaf");
leaf.GetComponent<Renderer>().material = leafColor;
}
}
make a material that you want to be the leaf material and put it in the slot
SIDENOTE: I also made a change to the time so it stops ticking after it hits 0 :)
That is unbelievably helpful! How would i select each leaf/gave each leaf the tag "Leaf" if i imported the tree from blender? i haven't gotten time to try this but I want to be sure im doing everything right so I don't screw anything up, yet again thank you!
If you imported it from blender i don't know. usually there is a part of the tree that has the leaves attached to it. if you're able to put the script directly on the leaves then put in this script:
public float targetTime = 60.0f;
public $$anonymous$$aterial leafColor;
void Update()
{
if (targetTime > 0)
{
targetTime -= Time.deltaTime;
}
if (targetTime <= 0.0f)
{
targetTime = 0.0f;
timerEnded();
}
}
void timerEnded()
{
gameObject.GetComponent<Renderer>().material = leafColor;
}
if you need anymore instruction/help on this message me on discord at: Bleakmountain50#1450
I have just sent you a friend request, I need some major help. Thanks again!!
Your answer
Follow this Question
Related Questions
How to create Timer animations? 1 Answer
How to make a random occurrences using a timer? 3 Answers
Countdown timer with textures ? 1 Answer
Convert this string formatting from C# to JS 1 Answer
Start timer with trigger 1 Answer