Coroutine not working even when game object is active.

I am making a player with a death animation, but whenever I call the animation it give me the error:

Coroutine couldn't be started because the the game object 'Player' is inactive!

This is my code for the animator:

 IEnumerator Death()
     {
     yield return null;
     trans.SetTrigger("Death");
     yield return new WaitForSeconds(.2f);

     SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);

 }
 // Start is called before the first frame update
 void Start()
 {
     gameObject.SetActive(true);
     rb = gameObject.GetComponent<Rigidbody2D>();
     
 }

Any help with this would be appreciated.

There are many things missing here. First of all the most important part is missing: Where and how do you actually start the coroutine. Keep in mind that the coroutine does not necessarily run on the object it belongs to but always runs on the object that you used to call StartCoroutine. StartCoroutine is a method of the MonoBehaviour base class. The coroutine will run on the object that you used to call StartCoroutine.

Next thing is this code makes no sense:

void Start()
{
    gameObject.SetActive(true);
    // [ ... ]

If the gameobject is not active, Start won’t be called at all. So the code would not run. If it is active then setting it active makes no sense since it is already active.

I assume that you try to run the coroutine on another object which you have either disabled / deactivated or destroyed. However once again you didn’t show us where and how you start your coroutine.

Another common error is to have a reference to a prefab that lives inside the project and does not represent an actual instance in the scene. You can call methods on prefabs, however prefabs are never “active”. They are just “blueprints” which you can instantiate into the scene.