MissingReferenceException: The object of type 'Animator' has been destroyed but you are still trying to access it.

Hi all. I know many other have already asked this kind of question, but still I haven’t found an answer which worked for me.

The issue is:

My game has 2 scenes so far: a Main Menu and a Level 1.

Normally, when my character dies in the game, she just respawns at the latest checkpoint.

But if I load Level 1 from the Main Menu, when she dies she respawns already dead with the Player Controller script disabled and I get this error:
“MissingReferenceException: The object of type ‘Animator’ has been destroyed but you are still trying to access it.”

With “already dead” I mean that she gets stuck in the dead animation and the input doesn’t work. I guess the input doesn’t work because one of the scripts is disabled. And she gets stuck in the animation because the controller is destroyed.
But what confuses me is that this happens only when playing this level loaded from the Main Menu.

This is the code that runs when she dies:

PLAYER CONTROLLER SCRIPT:

public void Die() //death is called by a KillingObject event
	{
		m_bShootingAllowed = false; //to avoid spamming calls to this function
        KillingObject.OnKill -= Die;
		Instantiate (DieEffect, m_oTransform.position + DieEffectPosition, m_oTransform.rotation);
		Debug.Log("Player is dead");
        m_oTimer.Start(deathAnimationLength + mk_fTimeToRespawn, Respawn, false, deathAnimationLength, HidePlayerModel);
    }
    
    private void HidePlayerModel()
    {
        Debug.Log("hiding player model");
        Renderer[] rs = GetComponentsInChildren<Renderer>();
        foreach (Renderer r in rs)
            r.enabled = false;
    }

    private void RestorePlayerModel()
    {
        Debug.Log("restoring player model");
        Renderer[] rs = GetComponentsInChildren<Renderer>();
        foreach (Renderer r in rs)
            r.enabled = true;
    }

    private void Respawn()
	{
		Debug.Log("Player respawned");
		Debug.Log (GameModule.GetInstance().GetCheckpointManager().GetRespawnPosition());
		m_oTransform.position = GameModule.GetInstance().GetCheckpointManager().GetRespawnPosition ();
        RestorePlayerModel();
        OnRespawn (); //this is a delegate
		KillingObject.OnKill += Die; //can die again
        AllowShooting();
	}

PLAYER ANIMATION SCRIPT:

    void OnDie()
    {
        KillingObject.OnKill -= OnDie; //to avoid spamming calls to this function
        _animator.Play("Death");
        source.clip = deathSounds[Random.Range(0, deathSounds.Length)];
        source.volume = .2f;
        source.Play();
    }
    
    void OnRespawn()
    {
        KillingObject.OnKill += OnDie;
        _animator.Play("Idle");
        source.volume = 1f;
    }

As you can see the player isn’t destroyed on death, the model is disabled and enabled after a while (set by the Timer script).

The scene is loaded with a simple LoadScene();.

Thanks in advance for any help :frowning:

Solved: I wasn’t detaching the callbacks on disable!

I Solved it by checking if Animator is not null before setTrigger

Animator anim;
if (anim != null){
anim.SetTrigger(“attack”);
}

I have this issue…

  1. MissingReferenceException: The object of type ‘Animator’ has been destroyed but you are still trying to access it.
    Your script should either check if it is null or you should not destroy the object.
    UnityEngine.Animator.SetBool (System.String name, Boolean value) (at C:/buildslave/unity/build/artifacts/generated/common/modules/Animation/AnimatorBindings.gen.cs:311)
    Alian_Script.OnTriggerStay2D (UnityEngine.Collider2D other) (at Assets/Scripts/Alian_Script.cs:70)

public class Alian_Script : MonoBehaviour {
public float enemySpeed;

Animator enemyAnimator;
//facing
public GameObject enemyGraphic;
bool canFlip = true;
bool facingRight = false;
float flipTime = 5f;
float nextFlipChance = 0f;
// attacking
public float chargeTime;
float startChargeTime;
bool charging , Dead;
Rigidbody2D enemyRB;

// Use this for initialization
void Start () {
	// if (this != null) Edited...here
	if("Enemy" == null) return;
	
enemyAnimator= GetComponentInChildren<Animator>();
enemyRB = GetComponent<Rigidbody2D>();
}

// Update is called once per frame
void Update () {
	if("Enemy" != null) return;
	/*if("Player" != null) return;*/

             if(Time.time > nextFlipChance){
              if(Random.Range (0,10)>=5) flipFacing();
               nextFlipChance = Time.time + flipTime;
                                           }
	
}

void OnTriggerEnter2D(Collider2D other)
{

if (other.tag == “Player”)
{

if(facingRight && other.transform.position.x < transform.position.x)
{
flipFacing();
}
else if (! facingRight && other.transform.position.x > transform.position.x)
{
flipFacing();
}
canFlip = false;
charging = true;
//Dead = true;
startChargeTime = Time.time + chargeTime;
}
}

void OnTriggerStay2D(Collider2D other){

if(other.tag == “Player”){
if(startChargeTime < Time.time)
{
if (! facingRight) enemyRB.AddForce(new Vector2(-1,0)*enemySpeed);//(new Vector2(-1,0)*enemySpeed);
else enemyRB.AddForce(new Vector2(1,0)*enemySpeed);//(new Vector2(1,0)*enemySpeed);
enemyAnimator.SetBool(“isCharging”,charging);
//enemyAnimator.SetTrigger(“isDead”);
}
}
}

void OnTriggerExit2D(Collider2D other){

if(other.tag == “Player”){
canFlip = true;
charging = false;
//Dead = true;
enemyRB.velocity = new Vector2(0f,0f);
enemyAnimator.SetBool(“isCharging”,charging);
//enemyAnimator.SetBool(“isDead”);
}
}

 void flipFacing()
                  {

if(!canFlip) return;
float facingX = enemyGraphic.transform.localScale.x;
facingX *= -1f;//facingX *= -1f;
enemyGraphic.transform.localScale = new Vector3(facingX, enemyGraphic.transform.localScale.y,enemyGraphic.transform.localScale.z);
facingRight = ! facingRight;
}

}

Please help me to find out…

can you tell the solution