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 /
This question was closed Jan 20, 2019 at 07:49 PM by zolyiaandras for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by zolyiaandras · Jan 20, 2019 at 02:14 PM · arrayoverlapspherefor loop

Destroy gameobjects in an array using a for loop.

As stated in the title, I am trying to destroy gameobjects that are within a certain range of my Player object at the moment I am calling the method. For some reason, after I run it once, it no longer works, no matter how much my ammo is, or how many times I press the key it is bound to. Anyone got any idea why it is happening?Thanks!

 private void Update()
 {
     if(Input.GetKeyDown(KeyCode.Space))
     {
         DestroyEnemies();
     }

     scoreBoard.text = $"SCORE : {Score}";
     ammunitionCount.text = $"AMMO : {Ammunition}";
 }

 //Enemy destroyer method
 private void DestroyEnemies()
 {
     LayerMask mask = LayerMask.GetMask("Ground");
     Collider[] enemiesInRange = Physics.OverlapSphere(this.transform.position, 10f, mask);

     if (enemiesInRange.Length != 0)
     {
         for (int i = 0; i < enemiesInRange.Length; i++)
         {
             if (Ammunition <= 0)
             {
                 break;
             }
             Destroy(enemiesInRange[i].gameObject);
             Debug.Log("Got one!");
             Ammunition--;
             Score++;
         }
     }
 }

 //Collectible objects destroyer
 private void OnCollisionEnter(Collision collision)
 {
     if(collision.gameObject.tag == "Collectible")
     {
         Destroy(collision.gameObject);
         Ammunition++;
     }
 }
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 tormentoarmagedoom · Jan 20, 2019 at 04:07 PM 1
Share

Good day.

Did you debugged the code? Is the method DestroyEnemies() beeing called ?

is this true?

 if (enemiesInRange.Length != 0)

and this?

 if (Ammunition <= 0)

and this is generating an array?

 Collider[] enemiesInRange = Physics.OverlapSphere(this.transform.position, 10f, mask);


As you can see, we can not help you if you come here, post a code, and ask... YOU are the only person that can find why is not working. You need to find what line is not giving you the expected result. And if even then you dont understand whay, then come to ask, why one specific comand or methid ios not doing what you think it shoiuld do.

Bye!

avatar image zolyiaandras tormentoarmagedoom · Jan 20, 2019 at 07:44 PM 0
Share

Ok, now that is embarrassing. Turns out I just didn't apply the proper Layer to the prefab I was instantiating, only to the Gameobject in the scene. Code works fine. Thanks for the suggestion to Debug every part of the method, that got me on the right track!

0 Replies

  • Sort: 

Follow this Question

Answers Answers and Comments

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

For Loop restarting early. Iteration variable restarting while loop condition is still true. 1 Answer

Please help! Audio not working in another class.. 0 Answers

Genetic Algorithm OverflowException: Number overflow: 1 Answer

SendMessage to Object in Array 1 Answer

Loading and Saving a Serializable Array. 1 Answer


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