• 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 Ectogenesis · Feb 27, 2018 at 05:46 PM · destroygameobject

How do i destroy whole army?

Hello everybody,im real newbie and sorry about that.

I m having a trouble with destroying enemies.I mean i can kill(destroy) everybody while throwing knife.But when i want to kill with melee attacks,im having this;

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

On the player side its my melee attack function:

 IEnumerator attack()
     {
         if (isGrounded && move == 0 && meleeAttackTimer)
         {
             meleeAttackTimer = false;
             if (!meleeAttackTimer)
                 isAttacking = true;
             if (canDoMeleeDamage)
             {
                 foreach (GameObject enemy in enemies)
                 {
                     this.enemy = enemy.GetComponent<Enemy>();
                     if (this.enemy.isAgainstPlayer)
                     {
                         this.enemy.takeDamage(meleeDamage);
                         Debug.Log("attacked");
                         yield return new WaitForSeconds(0.17f);
                         isAttacking = false;
                         if (!isAttacking)
                             yield return new WaitForSeconds(0.39f);
                         meleeAttackTimer = true;
                     }
                 }
             }
             else
             {
                 isAttacking = true;
                 yield return new WaitForSeconds(0.17f);
                 isAttacking = false;
                 if (!isAttacking)
                     yield return new WaitForSeconds(0.39f);
                 meleeAttackTimer = true;
             }
         }
     }

And it is die and takeDamage function from enemy side:

  public void takeDamage(int damage)
     {
         hp -= damage;
         if (hp <= 0)
         {
             StartCoroutine(die());
         }
     }
 IEnumerator die()
     {
         rigidBody2D.velocity = new Vector2(0, 0);
         isDead = true;
         yield return new WaitForSeconds(0.5f);
         Destroy(gameObject);
     }
 

How can i get rid of this situation? Thanks for supports.

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 browne11 · Feb 27, 2018 at 06:41 PM 0
Share

@Ectogenesis You are killing the enemy before you're coroutine is over in the attack sequence. See on your post where the line 15.

The issue appears to be your attack is getting hung up on line 17 and 20 waiting. The rest of your loop is sitting around till it's finish which you've probably attacked again and killed someone.

Try this ins$$anonymous$$d. I've moved the yields outside of the loop so you won't get caught trying to access enemies that are dead.

  IEnumerator attack()
          {
              if (isGrounded && move == 0 && meleeAttackTimer)
              {
                  meleeAttackTimer = false;
                  if (!meleeAttackTimer)
                      isAttacking = true;
                  if (canDo$$anonymous$$eleeDamage)
                  {
                      foreach (GameObject enemy in enemies)
                      {
                          this.enemy = enemy.GetComponent<Enemy>();
                          if (this.enemy.isAgainstPlayer)
                          {
                              this.enemy.takeDamage(meleeDamage);
                              Debug.Log("attacked");
                             
                          }
                      }
                       yield return new WaitForSeconds(0.17f);
                       isAttacking = false;
                       if (!isAttacking)
                       yield return new WaitForSeconds(0.39f);
                       meleeAttackTimer = true;
                  }
                  else
                  {
                      isAttacking = true;
                      yield return new WaitForSeconds(0.17f);
                      isAttacking = false;
                      if (!isAttacking)
                          yield return new WaitForSeconds(0.39f);
                      meleeAttackTimer = true;
                  }
              }
          }
 

Show more comments

2 Replies

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

Answer by browne11 · Feb 28, 2018 at 06:49 PM

@Ectogenesis I see the issue now. What is happening is your forloop is finding all gameobjects and is checking for your enemiy script which is probably missing. What you need to do is search game objects differently so only enemies are picked. You can revert back to what you had but keep the yield after the loop as I think it will work in a coroutine. If this is correct please mark it as your answer :)

Try this.

 IEnumerator attack()
           {
               if (isGrounded && move == 0 && meleeAttackTimer)
               {
                   meleeAttackTimer = false;
                   if (!meleeAttackTimer)
                       isAttacking = true;
                   if (canDoMeleeDamage)
                   {
                       foreach (var enemy in FindObjectsOfType<Enemy>())
                       {
                        
                           if (enemy.isAgainstPlayer)
                           {
                               enemy.takeDamage(meleeDamage);
                               Debug.Log("attacked");
                              
                           }
                       }
                        yield return new WaitForSeconds(0.17f);
                        isAttacking = false;
                        if (!isAttacking)
                        yield return new WaitForSeconds(0.39f);
                        meleeAttackTimer = true;
                   }
                   else
                   {
                       isAttacking = true;
                       yield return new WaitForSeconds(0.17f);
                       isAttacking = false;
                       if (!isAttacking)
                           yield return new WaitForSeconds(0.39f);
                       meleeAttackTimer = true;
                   }
               }
           }

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 Ectogenesis · Feb 28, 2018 at 08:45 PM 0
Share

Thanks for sharing experience. $$anonymous$$uch appreciate :)

avatar image
0

Answer by Ectogenesis · Feb 28, 2018 at 06:34 PM

Delete the yields on both sides and still same. foreach (GameObject enemy in enemies) { this.enemy = enemy.GetComponent<Enemy>(); if (this.enemy.isAgainstPlayer) { this.enemy.takeDamage(meleeDamage); Debug.Log("attacked"); } The error line is this; this.enemy = enemy.GetComponent();

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 browne11 · Feb 28, 2018 at 06:52 PM 0
Share

You should convert this to a comment as you listed it as an answer. Click the cogwheel on the right corner.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

75 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

Related Questions

Main Camera problem between two scenes 0 Answers

Timer for destroying/ appearing Gameobject (Vuforia) 2 Answers

How to instantiate and destroy objects with 2d trigger 1 Answer

How to play audio clip when destroying an object? 2 Answers

UI Object is there but can't destroy it. 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