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 dgknrdm · Mar 16, 2016 at 11:30 AM · c#vector3destroy objectdestroygameobject

Is there a way to Destroy a GameObject with given Coordinates?

Hi everyone,

I need to destroy a GameObject I've instantiated from a prefab. But I can't use "tag" to destroy it since it's tag is being used by other objects too. For example;

Think about 2 cubes side by side, I shoot 1 of them and it gets destroyed. There is no problem till this point. I can destroy the first object with "OnCollisionEnter" check. But the second object is the problem, because I want to destroy it at the same time the 1st one destroyed. The only unique thing I have about the second object is it's Vector3 coordinates.

Any help would be appreciated. Thanks in advance.

Have a good day.

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 TreyH · Mar 16, 2016 at 11:44 AM 0
Share

Is there a reason you're not just keeping track of that cube's transform via reference? How do you already know there's a cube beside the one you're destroying?

avatar image dgknrdm TreyH · Mar 16, 2016 at 11:49 AM 0
Share

I've created the cubes so I now they are there. And what do you mean by keeping track via reference. I'm new to Unity so it would be great if you were a little more specific, Thanks.

2 Replies

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

Answer by TreyH · Mar 16, 2016 at 11:58 AM

I think I see what you're going for? You can use OverlapSpheres http://docs.unity3d.com/ScriptReference/Physics.OverlapSphere.html.

 // Destroy an object at a location
 void DestroyAtPosition (Vector3 location)
 {
     // Pick some small search radius for your own unique situation
     float radius = 0.1f;
 
     // To guarantee results, you'll want to assign a unique Layer for the
     // cubes, and then do this with that layermask as a parameter
     
     // Get the cube sitting at our location
     Collider[] hitColliders = Physics.OverlapSphere (location, radius);
 
     // You can use a for loop more easily, but this will follow with
     // the given unity example api
     int i = 0;
 
     // Destroy everything in this list
     while (i < hitColliders.Length) {
         Destroy (hitColliders [i].gameObject);
         i++;
     }
 }


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 dgknrdm · Mar 16, 2016 at 12:14 PM 0
Share

I think this might work, Thanks for your help marked as answer.

avatar image
1

Answer by Ali-hatem · Mar 16, 2016 at 11:45 AM

 Destroy (GameObject.FindWithTag("cubes "));
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 dgknrdm · Mar 16, 2016 at 11:51 AM 0
Share

Like I said I can't use tag because it's tag is being used by other objects too. So I don't want to destroy the other objects I just want to destroy these two.

avatar image Ali-hatem dgknrdm · Mar 16, 2016 at 12:24 PM 0
Share

But the second object is the problem, because I want to destroy it at the same time the 1st one destroyed.

i thought you want to destroy all
avatar image dgknrdm Ali-hatem · Mar 17, 2016 at 01:36 AM 0
Share

Sure but there are other objects with the same tag which I don't want to destroy , I should've wrote that my bad :)

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

122 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

Related Questions

How to destroy object after it moves out of screen 7 Answers

Why i can't acces a global list in my class from OnDestroy() function 0 Answers

Set new default position each time? 1 Answer

How to fix MissingReferenceException? 0 Answers

Applying a script to two objects not working 2 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