WaitForSeconds(...) in while loop

Hi!
I’ve wrote a function which should fade a material:

	private IEnumerator FadeOut(Material material)
	{
		while (material.color.a > 0) 
		{
			material.color = new Color(material.color.r, material.color.g, material.color.b, material.color.a - 1);
			yield return new WaitForSeconds(0.5f);
		}
	}

My problem is that the WaitForSeconds line doesn’t work - after call this function (using StartCoroutine(…)) alpha of my material immediately sets to 0. Can You tell me why?

The values in the Color for rgba are 0 to 1. You might want to use Color32. Or subtract a small value from alpha say 0.05.

See the scripting reference:

Color: RGBA values are floating point values from 0 to 1.

Color32: Each color component is a byte value with a range from 0 to 255.

You are subtracting 1 from the alpha in the color which sets alpha to 0 in the first execution.

Values of alpha defines what percentage of light can pass through the material i.e. between 0 and 1 where 0 = 0% and 1 = 100%. You are setting it to 0 in one step, hence it happen at once.