Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
1
Question by opporo · Oct 04, 2016 at 04:09 PM · animatorloadmissingreferenceexceptiondestroyed

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 :(

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image opporo · Oct 04, 2016 at 04:16 PM 0
Share

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)*

avatar image opporo · Oct 04, 2016 at 05:01 PM 0
Share

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?)

4 Replies

· Add your reply
  • Sort: 
avatar image
8
Best Answer

Answer by opporo · Oct 14, 2016 at 02:37 PM

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

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image satanas · Dec 28, 2016 at 10:58 PM 0
Share

Thanks! You saved me with that answer

avatar image davidhaley · Apr 30, 2017 at 03:01 AM 0
Share

Dude; you saved me. Thank you!

avatar image wightwhale · Aug 23, 2017 at 11:09 PM 0
Share

Oh man this is such a non obvious error. Thanks!

avatar image
2

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"); }

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image monkey5135 · Mar 29, 2021 at 09:16 PM 0
Share

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?

avatar image
0

Answer by varghesechandhutk · Oct 10, 2018 at 12:00 PM

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...

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by DiogoAraujo · May 31, 2019 at 06:54 PM

can you tell the solution

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

81 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Code still trying to access Object that has been destroyed. 1 Answer

Unet load testing 0 Answers

Load Scene and keep Enemy and Puzzles dead/done 1 Answer

Loading contents of a folder as AssetBundle 1 Answer

Loading data from File using TextAsset. Unable to output ÖÄÅ chars,Loading data from File using TextAsset, but unable ouput ÖÄÅ chars to UI.text 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges