I’m implementing some code that allows an object to blend to a specific color over a short period of time.
Here is an example of how the coroutine is called:
StartCoroutine(ColorChange(obj.GetComponent<Renderer>(), Color.cyan));
The color is the color to blend to.
Here is the coroutine:
IEnumerator ColorChange(Renderer rend, Color col) {
var startTime = Time.time;
var colStart = rend.material.color;
float t = 0f;
while (t < 1f) {
t = (Time.time - startTime) / 0.25f;
rend.material.color = Color.Lerp (colStart, col, t);
yield return null;
}
}
This was working fine when a single object was using this bit of code, and it didn’t have to worry about it being called while it was already changing.
The objects that get this color fade are only temporarily referenced and then (the reference) is discarded, making way for the next object. Multiple objects are being fed through this coroutine from inside the same script, causing obvious problems.
Is there a way to have a function like this that can be called by multiple objects at anytime without all the objects needing their own copy of the code? There has to be an efficient way of doing this, without having to copy and paste this code so that every single object gets it’s own copy.
Thanks as always.