Lerping a material _TintColor: why does it work in a case and not the other?

Hi everyone.

I’m building some simple FX effects by changing material properties on some FX meshes.

I have the follwing code that works perfectly:

	public class FX_buttons : MonoBehaviour { 
	public Color colorFull;
	public Color colorEmpty;
	public float fadeTime = 1f;
	public float smoothness = 0.02f;
	float t = 0;

    void Update () {
    	gameObject.GetComponent<Renderer> ().material.SetColor ("_TintColor", Fade(colorEmpty, colorFull, smoothness, fadeTime));
		}

	/// <summary>
	/// Fade from startColor to endColor in duration seconds.
	/// </summary>
	/// <param name="startColor">Start color.</param>
	/// <param name="endColor">End color.</param>
	/// <param name="duration">Duration in seconds.</param>
    Color Fade (Color startColor, Color endColor, float smoothness, float duration)
    {
    	Color c = startColor;
		float increment = smoothness/duration;
		while (t <1)
		{
			c = Color.Lerp(startColor, endColor, t);
			t += increment;
			return(c);
		}

		return (endColor);
    }

However, I tried another Fade definition as follows:

void Fade (Material material, Color startColor, Color endColor, float smoothness, float duration)
	{
		Color c;
		float increment = smoothness/duration;
		while (t <1)
		{
			c = Color.Lerp(startColor, endColor, t);
			t += increment;
			material.SetColor("_TintColor", c);
		}
	}

and using this Method in the Update()…

 void Update () {
		Fade (gameObject.GetComponent<Renderer>().material, colorEmpty, colorFull, smoothness, fadeTime);
    }

And it doesn’t actually lerp. The Material’s _TintColor simply passes from one to the other instantaneously… and I can’t quite figure out why… any ideas?

while (t <1) {
c = Color.Lerp(startColor, endColor, t);
t += increment;
material.SetColor(“_TintColor”, c);
}

Unity runs all user code in a single thread, which means that at any given time only one line of code is being run. So when you write a loop like this, the execution runs only the code in the loop until t goes over 1 and subsequently the color fades all the way to the end.

“Only the code in the loop” meaning that not a single other MonoBehaviour can run their Update at the same time, the screen cannot be repainted etc. etc.

In your working code you have a return statement inside the while-loop so it will only ever “loop” once. It’s effectively an if-statement though you used “while”.

Since you call the Fade() function inside Update(), you should basically use “if” in both versions of the function. What you see on the screen only gets repainted between Update() calls so it almost never makes sense to change color or do any other visual modifications in a loop: the objects state only gets drawn on the screen after exiting the loop (which you have experienced at first hand here)

Have you tried what happens when you put an endless loop into your code ?

while (true);

Whole Unity freezes. It gets stuck executing that one line in your code and it can’t repaint or gather input from buttons so there’s no way to make it stop. All you can do is close it from task manager.