Toggling a sprite renderer?

I need to be able to turn a sprite renderer on and off for my player hurt effect. The title mostly says it all.

So, how do I toggle the sprite renderer on an object from a script?

Oh, and if this changes anything, I want to do it in c#.

Here’s a code using a C# coroutine.

You can copy the code you need to make it longer, and change the wait for second to make it faster or slower.
Also make it cleaner by saving the renderer in a variable,
Allow the Coroutine to take an argument (probably the delay), so you can control the speed of the flickering.

void Start(){
		StartCoroutine("ToggleRenderer");
	}

	IEnumerator ToggleRenderer(){
		yield return new WaitForSeconds(0.5f);
		this.gameObject.GetComponent<SpriteRenderer>().enabled = false;
		yield return new WaitForSeconds(0.5f);
		this.gameObject.GetComponent<SpriteRenderer>().enabled = true;
		yield return new WaitForSeconds(0.5f);
		this.gameObject.GetComponent<SpriteRenderer>().enabled = false;
		yield return new WaitForSeconds(0.5f);
		this.gameObject.GetComponent<SpriteRenderer>().enabled = true;
		//Repeat as much as needed
	}

Another suggestion: Change object, eg.: when the player dies.

`
    	void Die()
    	{
    		manager.deadPlayer = true;
    		Instantiate (playerDead, transform.position, Quaternion.identity);
    		Destroy (gameObject);
    	}
`

Know this package: Unity Asset Store - The Best Assets for Game Making