Why does this matter?

So I have 2 different projectiles hitting an enemy.

1 gets reused and just goes off screen in an array, while the other destroys itself on contact.

Both call a function on the enemy to “flash hit” (flashes the enemies color to red to show it’s been hit)

However, the one that destroys itself, the enemy only starts the “flash” and doesn’t go back to normal.

If i put a delay before the destruction of the projectile, then it fully plays out the flash. So my question is, why does it matter? Should FlashHit () run independantly of the object that called it? Why doesn’t it just run on it’s own until completion?

The flash function for the enemy;

function FlashHit ()
	var startTime : float = Time.time;
	while (Time.time < startTime + hitFlash)
		myAnimation.SetMeshColor(Color.Lerp (hitColor, normalColor, (Time.time - startTime)/hitFlash));

It happens because even though the coroutine FlashHit () is present on the enemy object it is called from the destroyed object and once the script calling the coroutine is destroyed the coroutine is stalled which in your case the destroyed object is destroyed so the calling script is no more available for the coroutine to return the yield to so the coroutine stops the execution. Hence your enemy remains red.

For more information you can check this question: What can cause a coroutine to not being working?