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 Kacer · Mar 16, 2011 at 02:36 PM · destroycollidersgameobjectsmultipletouching

Destroying objects with the same name that are touching eachother.

Hello

I have already been searching this site and google for related answers, and i have found none, but if anyone can find some, then just link it here and call me a noob :).

Anyway, what i want to do, is to click on a gameobject with a box collider on it, and delete destroy eventual gameobjects which are touching the first gameobject.

i know i can use 'OnCollisionEnter' to destroy objects when they touch eachother, but that happens immediately when they come into contact, i want to wait till a button is pressed.

i have tried making a variable with the name of the gameobjects that are touching the object, but for some reason it didnt work out for me, it deleted the object i pressed, and 1 other object, always the same. but it didnt work as intended, as it didnt matter if the object was touching the object i was pressing.

I hope someone is able to help me, tell me if im rambling, and i'll try to clarify it a bit.

Thanks in Advance.

-Seth

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

1 Reply

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

Answer by Statement · Mar 16, 2011 at 02:56 PM

Anyway, what i want to do, is to click on a gameobject with a box collider on it, and delete destroy eventual gameobjects which are touching the first gameobject.

See OnCollisionStay, OnMouseDown.

bool destroy = false;

IEnumerator OnMouseDown() { destroy = true; // Let object know we should destroy. yield return null; // Wait one frame so physics can do its stuff. destroy = false; // Don't allow destruction after one frame. }

void OnCollisionStay(Collision collision) { if (destroy && collision.gameObject.name == name) { Destroy(collision.gameObject); } }

Or JS, if you prefer:

var destroy : boolean = false;

function OnMouseDown() { destroy = true; // Let object know we should destroy. yield; // Wait one frame so physics can do its stuff. destroy = false; // Don't allow destruction after one frame. }

function OnCollisionStay(collision : Collision) { if (destroy && collision.gameObject.name == name) { Destroy(collision.gameObject); } }

Note that colliders require a rigidbody or character controller in order for events to fire.

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

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

No one has followed this question yet.

Related Questions

Destroying objects within collision 1 Answer

[C#] Translate.position a lot of gameobjects at once (due to velocity limitations) 0 Answers

Find gameobjects by tag, and destroy if in the distance of a certain game object... 1 Answer

multiple collisions to Destroy (gameObject); 1 Answer

is there a way to count how many gameobjects are adjacent to each other and have the same tag? 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