Need to Change Sprite Color in Coroutine Using LateUpdate
I have a coroutine in my player health script that allows temporary invincibility upon getting hit. When this triggers, I'm trying to get the alpha value of the player to change along with it. Unfortunately Unity has the issue that the sprite renderer properties can't change as long as the objects has an Animator attached, because the Animator always has priority in the current updated frame. I've done some research on how to get around this and apparently there's a way to be able to change the sprite renderer properties if you use LateUpdate. I'm wondering if anyone has any suggestions as a way that I can call my coroutine using LateUpdate so that my player will actually have its alpha channel changed regardless of the Animator. If there's a better way to have the sprite renderer take priority over the animator, I'm interested in hearing that as well. I'd love to utilize this in other scripts, like having enemies change color when taking damage, so hopefully there exists a solution to this weird issue.
The relevant part of my current health script:
void OnCollisionEnter2D(Collision2D collision)
{
if (collision.gameObject.tag == "Enemy1" && currentHealth > 0)
{
StartCoroutine ("GetInvulnerable");
}
}
IEnumerator GetInvulnerable()
{
Physics2D.IgnoreLayerCollision (8, 12, true);
color.a = 0.5f;
rend.color = color;
yield return new WaitForSeconds (3f);
Physics2D.IgnoreLayerCollision (8, 12, false);
color.a = 1f;
rend.color = color;
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613030549im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Sprite changes only briefly? 0 Answers
Reset animation in animator when execution the same animation in row 0 Answers
Audiosource.Play not playing, but causing issues in coroutine 1 Answer
Getting variable from script while keeping other code from it 1 Answer
How to deactivate a dropdown list when changing UI tabs 0 Answers