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
0
Question by RobertHayes · Nov 23, 2015 at 08:18 PM · transform.positiontagsdestroy object

How Do I Destroy All Objects At a Given Location?

I want to destroy all objects at a particular location. The way I am currently doing it is:

 void DestroyObjectAtLocation(float nX, float nY, float nZ) {
 
     foreach (string strTag in UnityEditorInternal.InternalEditorUtility.tags) 
               {
         GameObject[] tiles = GameObject.FindGameObjectsWithTag (strTag);
         foreach (GameObject tile in tiles)
             if ((tile.transform.position.z == nZ) && (tile.transform.position.x == nX) && (tile.transform.position.y == nY))
                 Destroy (tile);
     }    
 }

This works perfectly but seems extremely inefficient. Is there a better way? I am largely a novice so please have mercy on my poor soul .

Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
3

Answer by fafase · Nov 23, 2015 at 08:23 PM

Use a trigger sphere or box. Place it at the position, make it as large as needed and use:

 void OnTriggerEnter(Collider col){
      if(col.collider.gameObject.CompareTag(strTag)){Destroy(this.gameObject);}
 }

Add this to the tiles and mark the collider as trigger.

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 RobertHayes · Nov 25, 2015 at 01:17 AM 0
Share

I don't quite follow you. Could you make it a little more step by step? (Novice, sorry.)

avatar image
0

Answer by wfefrfe · Nov 25, 2015 at 03:16 PM

void DestroyObjectAtLocation(float nX, float nY, float nZ, float minDist) {

     Vector3 tmpLocation = new Vector3 (nX, nY, nZ);
     Transform[] tiles = GameObject.FindObjectsOfType<Transform> ();

     for (int i = 0; i < tiles.Length; i++) {
         if(Vector3.Distance(tiles[i].position, tmpLocation) <= minDist){
             Destroy(tiles[i].gameObject);
         }
     }
 }
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 RobertHayes · Nov 26, 2015 at 12:07 AM 0
Share

That works like a champ and is also a lot more adaptable; I can use it to eli$$anonymous$$ate whole areas if needed. Thanks!

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Grabbing Object Trouble 0 Answers

Making objects with the same tag destroy eachother 1 Answer

transform position don't work with scaled parent 0 Answers

How to find the position 1 distance away from, and pointing to another object? 0 Answers

Main Camera automatically increment by 0.75 on Y axis 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