- Home /
How can i color a object material color using StartCoroutine ?
It's working in general but the speed(Duration) is too fast and never the time i set in the Inspector.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ChangeScale : MonoBehaviour
{
public GameObject objectToScale;
public float duration = 1f;
public Vector3 minSize;
public Vector3 maxSize;
private bool scaleUp = false;
private Coroutine scaleCoroutine;
public Color startColor;
public Color endColor;
public float colorDuration; // duration in seconds
private void Start()
{
startColor = GetComponent<Renderer>().material.color;
endColor = Color.green;
objectToScale.transform.localScale = minSize;
}
// Use this for initialization
void Update()
{
if (Input.GetKeyDown(KeyCode.F))
{
//Flip the scale direction when F key is pressed
scaleUp = !scaleUp;
//Stop old coroutine
if (scaleCoroutine != null)
StopCoroutine(scaleCoroutine);
//Scale up
if (scaleUp)
{
//Start new coroutine and scale up within 5 seconds and return the coroutine reference
scaleCoroutine = StartCoroutine(scaleOverTime(objectToScale, maxSize, duration));
}
//Scale Down
else
{
//Start new coroutine and scale up within 5 seconds and return the coroutine reference
scaleCoroutine = StartCoroutine(scaleOverTime(objectToScale, minSize, duration));
}
}
if (Input.GetKeyDown(KeyCode.C))
{
StartCoroutine(ChangeColor());
}
}
IEnumerator scaleOverTime(GameObject targetObj, Vector3 toScale, float duration)
{
float counter = 0;
//Get the current scale of the object to be scaled
Vector3 startScaleSize = targetObj.transform.localScale;
while (counter < duration)
{
counter += Time.deltaTime;
targetObj.transform.localScale = Vector3.Lerp(startScaleSize, toScale, counter / duration);
yield return null;
}
}
IEnumerator ChangeColor()
{
float t = 0;
while (t < colorDuration)
{
t += Time.deltaTime;
GetComponent<Renderer>().material.color = Color.Lerp(startColor, endColor, t / colorDuration);
yield return null;
}
}
}
When i press on C i want it to change the object color with speed depending on the duration i set. In the Inspector i set the colorDuration to 0.1 or to 10 but it's not effecting it. It keep changing the color in same speed it's never 10 seconds if i set it to 10 in the Inspector.
The object i want to change it's color have some childs with Renderers so i attached ther script to some of the childs not to only one object.
You can see in the screenshot the object i want to color is the NAVI(Droid) And each child polySurface have a Renderer. polySurface33 and 35 and 34 they all have renderer and i attached the script to all of them also to the droid_glass and droid_eye
So in general it's working but it's not working with the colorDuration. It's never 10 seconds or never 0.1f second.
Answer by Bieere · Dec 25, 2017 at 07:36 PM
What i would recommend, is sending the material to the Coroutine, and then it can adjust the colour straight in there.
private Material material;
void Start()
{
[...]
material = GetComponent<Renderer>().material;
}
void Update()
{
[...]
if (Input.GetKeyDown(KeyCode.C))
{
StartCoroutine(ChangeColor(material));
}
}
IEnumerator ChangeColor(Material toChange)
{
float t = 0;
while (t < colorDuration)
{
t += Time.deltaTime;
toChange.color = Color.Lerp(startColor, endColor, t / colorDuration);
yield return null;
}
}