Make a 2D sprite flash?

I’m trying to get my player to flash invisible and visible while it’s invulnerable after being hit. Presently, the player is hit and becomes invulnerable for a short time as intended, however it just becomes invisible for as long as it is invulnerable. I wanted to know if there was a way to make a bool, in this instance an “IsVisible” bool, that would toggle on and off every X amount of seconds while another bool is true. Basically, the “IsVisible” would toggle on and cause the sprite to render, toggle off and cause the sprite to not render, on an interval so that it flashed rendered and not rendered. All help would be appreciated. I’m trying to polish up my game for showcase on Sunday. Thanks.

    if (invulnerable)
    {
        player.GetComponent<SpriteRenderer>().enabled = false;
    }
    else
    {
        player.GetComponent<SpriteRenderer>().enabled = true;
    }

public int delay =100;
public SpriteRenderer mySpriteRenderer;
int counter;
bool toggle=false;

	void Update()    // you can you FixedUpdate for fixed flash rate
	{
		Flash(mySpriteRenderer);
	}
	
	void Flash(SpriteRenderer spriteRen)
	{
		
		
		if(counter>=delay)
		{ 
			counter = 0;
			
			toggle=!toggle;

			if(toggle)
			{
			 	spriteRen.enabled=true;
			}
			else
			{
				spriteRen.enabled=false;
			}
			
		}
		else
		{
			counter++;
		}
	}

@KiethFurry I know this is old, but, this would probably work.

This is what I referenced.

In the unity editor: Have a collider (box2d,circle2d,poly2d,whatver) on your player, and whatever is hitting your player. Set them both as triggers. If you have multiple objects that you want to have different effects on the player, you can use a tag. Otherwise disregard my “if(other.gameobject.tag)” statement. Note that in my experience using this method, if an untagged object hits your player, the game breaks.

Script Explanation: Make a variable for the players spriterenderer, and it’s collider. Get those components in your start function so you’re actually referencing something. Use “void OnTriggerEnter2D” to look for the collision. Collider2D Other is what is hitting the player. This is where you can look for tags or not. You would then call the “IEnumerator Flash()”. To call you have to use “StartCorutine(information)”.

Flash() Explanation: Use the loop to determine how many times you want your player to flash. The time between each flash is determined by “x”. “x” is passed into the function when it’s called as “flashSpeed”, which can be set from editor for tweaking.

public float flashSpeed;

PolygonCollider2D polyColl;
SpriteRenderer spRndrer;

void Start() {
    polyColl = GetComponent<PolygonCollider2D>();
    spRndrer = GetComponent<SpriteRenderer>();
}

void OnTriggerEnter2D(Collider2D other) {
    if (other.gameObject.tag == "objectThatHitsMeTag")
        StartCoroutine(Flash(flashSpeed));
}

IEnumerator Flash(float x) {
    polyColl.enabled = false;

    for (int i = 0; i < 10; i++) {
        spRndrer.enabled = false;
        yield return new WaitForSeconds(x);
        spRndrer.enabled = true;
        yield return new WaitForSeconds(x);
    }

    polyColl.enabled = true;
}