Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
0
Question by rjayant · Aug 10, 2020 at 09:49 PM · error messagemissingreferenceexception

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

I'm getting this error when I run my game: MissingReferenceException: The object of type 'CircleCollider2D' 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.

When I run my game, everything works perfectly fine and I'm not sure where this error is coming from.. I was hoping one of you amazing people could take a look and shed some light!

Here is the code:

 public class EnemyAttackGen1 : MonoBehaviour {
     public GameObject attackEffect;
     public int damageAmmount = 20;
 
     IEnumerator OnTriggerEnter2D(Collider2D enemy) {
         if (enemy.tag == "Enemy") {
             yield return new WaitForSeconds(0.5f);
             
             if (GetComponent<Generator>().open) {
                 GetComponent<Generator>().currentHealth -= damageAmmount;
             }
             
             Instantiate (attackEffect, enemy.gameObject.transform.position, enemy.gameObject.transform.rotation);
             Destroy(enemy.gameObject);
         }
     }
 }


Let me know if you need more information! Thank you in advance

Comment
Add comment · Show 3
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 FlaSh-G · Aug 11, 2020 at 09:01 PM 0
Share

Sounds like your enemy is destroyed within the 0.5 seconds of wait time that you have. Not sure though, it's hard to tell.

avatar image unity_pYHQP2dVdNqmfA · Aug 12, 2020 at 09:22 AM 0
Share

I am having the same error I dont understand why my program is calling destroyed object

avatar image VladimirTa unity_pYHQP2dVdNqmfA · Aug 12, 2020 at 06:41 PM 0
Share

We can't help if you don't show the portion of the script that is causing the error and also the error itself.

1 Reply

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

Answer by VladimirTa · Aug 12, 2020 at 01:04 AM

That's because you are colliding with the same "enemy" more than once. While the coroutine is waiting 0.5 seconds, you keep colliding with the same object, and when the time comes for the same object the second time, it is already destroyed. You need to add some checks to make sure to collide with the enemy only once. Maybe disabling the collider on the enemy before waiting. Also some optimization tips. If you are using WaitForSeconds more then once, instead of using a new one each time, cache it first, you will save some resources on garbage collection.

Comment
Add comment · Show 2 · 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 rjayant · Aug 12, 2020 at 02:28 AM 0
Share

This comment helped me understand why the error was co$$anonymous$$g up. Thank you so much, the issue has been resolved on my end!

avatar image VladimirTa rjayant · Aug 12, 2020 at 02:37 AM 0
Share

Glad I was able to help. Have a nice day!

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

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

How can throw "Missing reference exception" this gameObject?.Getcomponent<>() ?? 2 Answers

MissingReferenceException: The object of type 'GameObject' 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. 0 Answers

Powerup only spawning twice (MissingReferenceException Error) 2 Answers

Particle Emitter error, Missing reference exception. 1 Answer

Unassigned Reference Exception ? 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