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

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

No one has followed this question yet.

Related Questions

Turn off all Colliders from Scene 1 Answer

is there a way to count how many gameobjects are adjacent to each other and have the same tag? 1 Answer

I am using this script to drag/move gameobjects with the mouse. I need the script to ONLY recognize objects with a TAG, like "Painting", How can I fix this? 3 Answers

onCollision Destroy 1 Answer

Best way to include multiple game themes into the game each with different game objects? 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges