Sprite Alpha Color Won't Change Via Script

Update: It’s a problem with the Animator taking priority over the sprite renderer. I’m looking into some kind of solution.

I’m trying to code some invincibility frames in my health script and I’m trying to set the Player’s alpha value to half while in those frames. The ignoring collision between layers works fine on collision so I know the CoRoutine is running, but I can’t get the alpha value to change on runtime. It’s at the bottom of the script under the “GetInvulnerable” command. Is it perhaps something wrong with the way I’m calling the Renderer?

public class Health : MonoBehaviour
{
    public Image healthBar;

    [SerializeField]
    private float maxHealth = 100;
    private float currentHealth;
    float lerpSpeed;
    Renderer rend;
    Color color;
    

    private void Start()
    {
	currentHealth = maxHealth;
	rend = GetComponent<SpriteRenderer> ();
	color = rend.material.color = color;
    }

    private void Update()

    {
	if (currentHealth > maxHealth) currentHealth = maxHealth;

	HealthBarFiller();
	lerpSpeed = 3f * Time.deltaTime;
    }
    

//SMOOTH HEALTH BAR
    void HealthBarFiller()
    {
	healthBar.fillAmount = Mathf.Lerp(healthBar.fillAmount, currentHealth / maxHealth, lerpSpeed);
    }

    //DAMAGE
    public void Damage (float amount)
    {	

	currentHealth -= amount;

	if(currentHealth <= 0.0f)
	{
	    Die();
	}
    }

    //HEAL
    public void Heal(float amount)
    {
	if (currentHealth < maxHealth)
	{
	    currentHealth += amount;
	}
	else
	{
	    currentHealth = maxHealth;
	}
    }

    private void Die()
    {
	//transition to Death Screen
    }

    //Invulnerable
    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.material.color = color;
	yield return new WaitForSeconds (2f);
	Physics2D.IgnoreLayerCollision (8, 12, false);
	color.a = 1f;
	rend.material.color = color;
    }
	
}

couple things to try at first glance. at the top of your class try changing “Renderer rend;” to “SpriteRenderer rend;”. Then in start do “color = rend.color;” (dont add the extra “= color” at the end). Finally at the bottom of the script replace both instances of “rend.material.color” with “rend.color”. Also one thing to keep in mind is that if your sprite on your player is a child of the player game object then in the start method you would have to do "rend = GetComponentInChildren<SpriteRenderer>();"


Let me know how that works