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 :(
The complete error:
*$$anonymous$$issingReferenceException: 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.Play (System.String stateName, Int32 layer, Single normalizedTime) (at C:/buildslave/unity/build/artifacts/generated/common/modules/Animation/AnimatorBindings.gen.cs:934) UnityEngine.Animator.Play (System.String stateName) (at C:/buildslave/unity/build/artifacts/generated/common/modules/Animation/AnimatorBindings.gen.cs:929)
PlayerAnimation.OnRespawn () (at Assets/Resources/Scripts/Player/PlayerAnimation.cs:216)
Player.Respawn () (at Assets/Resources/Scripts/Player/Player.cs:109)
Timer.FinishTimer () (at Assets/Resources/Scripts/Utils/Timer.cs:46)
Timer.ComputeTimer () (at Assets/Resources/Scripts/Utils/Timer.cs:36)
Timer.Update () (at Assets/Resources/Scripts/Utils/Timer.cs:19)*
In the PlayerController script I also have these methods:
private void OnEnable()
{
$$anonymous$$illingObject.On$$anonymous$$ill += Die;
}
private void OnDisable()
{
$$anonymous$$illingObject.On$$anonymous$$ill -= Die;
}
$$anonymous$$y question is, does the disabling of renderers count as OnEnable()?
In other words, am I accidentally attaching the events twice? (and if so, why would this happen only when I load the scene from the menu?)
Answer by opporo · Oct 14, 2016 at 02:37 PM
Solved: I wasn't detaching the callbacks on disable!
Answer by unity_31130666 · May 11, 2020 at 11:10 AM
I Solved it by checking if Animator is not null before setTrigger
Animator anim; if (anim != null){ anim.SetTrigger("attack"); }
Thanks for this. I am not entirely sure why the animator isn't detroyed with the instance of the object but your code solved the crashes. I am worried there might be things piling up in the back then though. Is this likely to cause a crash down the line?
Answer by varghesechandhutk · Oct 10, 2018 at 12:00 PM
I have this issue....
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...
Your answer
![](https://koobas.hobune.stream/wayback/20220612094008im_/https://answers.unity.com/themes/thub/images/avi.jpg)