I have my 2D player animations playing on two separate game objects under the same parent. I’m trying to use the parent script to cause the sprite renderers on the children objects to enable and disable, aka to ‘flash’ when damaged.
I’m new to C# and Unity, so I’m struggling. With the following script, nothing is happening. Can someone please help?
IEnumerator playerFlash()
{
gameObject.GetComponentInChildren<Renderer>().enabled = false;
yield return new WaitForSeconds(.5f);
gameObject.GetComponentInChildren<Renderer>().enabled = true;
yield return new WaitForSeconds(.5f);
gameObject.GetComponentInChildren<Renderer>().enabled = false;
yield return new WaitForSeconds(.5f);
gameObject.GetComponentInChildren<Renderer>().enabled = true;
yield return new WaitForSeconds(.5f);
gameObject.GetComponentInChildren<Renderer>().enabled = false;
yield return new WaitForSeconds(.5f);
gameObject.GetComponentInChildren<Renderer>().enabled = true;
}
public void DamageKnockback(Vector3 knockbackDir, float knockbackDistance, int damageAmount)
{
transform.position += knockbackDir * knockbackDistance;
HeartsHealthVisual.heartsHealthSystemStatic.Damage(damageAmount);
StartCoroutine(playerFlash());
Debug.Log("Knocked right the fuck back, bitch.");
}