I have written a coroutine that handles the texture change.
Here is the coroutine
public static Coroutine Init_textureChange;
public void TriggerTextureChange(Renderer [] renderers, Texture texture)
{
if (Init_textureChange != null)
{
Debug.Log("Coroutine is: "+Init_textureChange);
StopCoroutine(Init_textureChange);
//Set2DefaultTexture(renderers);
}
Init_textureChange = StartCoroutine(InitTextureChange(renderers, texture));
}
private IEnumerator InitTextureChange(Renderer[] renderers, Texture texture)
{
float duration = 0.2f;
Debug.Log("Init texture change");
//Applying damage texture.
foreach(Renderer renderer in renderers)
{
Debug.Log("Count:" + count);
renderer.material.SetTexture("_Detail", texture);
//renderer.material.SetColor("_Color", Color.red);
}
Debug.Log("Texture applied");
yield return new WaitForSeconds(duration);
//Reset the texture to normal
foreach (Renderer renderer in renderers)
{
Debug.Log("Count:" + count);
renderer.material.SetTexture("_Detail", null);
//renderer.material.SetColor("_Color", Color.white);
}
Debug.Log("Texture set to normal");
}
But sometimes coroutine doesn’t execute completely and stops before “yield” line.