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
1
Question by nt314p · Sep 05, 2017 at 12:17 AM · colliderprefabdestroy

How to destroy GameObject if it overlaps another GameObject

I have a prefab that is being spawned in in a random location. This prefab has only a box collider attached to it and a script. The collider is not a trigger.

How can I destroy the prefab if it intersects with another prefab?

The problem that I am having is that when the prefab spawns in, it DOES NOT trigger the collision and thus I cannot destroy the gameobject. onCollisionEnter DOES NOT WORK.

BONUS: Make sure only one prefab destroys itself, not both.

If you need any more info, just ask! Please guys, I'm desperate and I don't think I can make a workaround.

I will give you 30 points if you answer the question!

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 nt314p · Oct 06, 2017 at 10:19 PM 0
Share

Shameful bump :(

avatar image nt314p nt314p · Nov 04, 2017 at 07:07 PM 0
Share

Another shameful bump. There's only so many ways I can google something...

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by MacDx · Sep 05, 2017 at 02:14 AM

Since you said "overlap" I'll assume you are working with triggers.

Create a tag and then add this method to the prefab's attached script.

 void OnTriggerEnter(Collider other)
 {
     if(other.CompareTag("yourCreatedTag")
     {
         Destroy(this.gameObject);
     }
 }

This will destroy the prefab when it overlaps with an object that has that specific tag assigned. You could also check if the "other" object contains a specific component instead of a tag.

If you want them to be of the same type and have only one of them be destroyed then you need to assign them some kind of priority. You could do a check to see which one was going faster, or give them priority value so each of them would do a comparison of this value and then act accordingly.

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 nt314p · Sep 05, 2017 at 10:15 PM 0
Share

Ah, I am not using triggers as the player in my game needs to collide with them.

Also, I have already tried this method, but it doesn't work. When the prefab spawns in, it doesn't trigger the collider.

But, thanks for the effort anyway.

avatar image MacDx nt314p · Sep 06, 2017 at 01:23 AM 0
Share

If you are not using triggers then it's obvious that this method won't work lol, that's why it's called OnTriggerEnter. Try replacing OnTriggerEnter with OnCollisionEnter

like this:

 void OnCollisionEnter(Collision collision)
 {
         if (collision.gameObject.CompareTag("yourCreatedTag"))
             Destroy(this.gameObject);
 }

P.S. make sure that you properly tagged the objects

avatar image nt314p MacDx · Sep 08, 2017 at 10:28 PM 0
Share

When the prefab spawns in, it doesn't trigger a collision. This is the most frustrating part.

avatar image
0

Answer by Saripsis · Oct 02, 2017 at 10:16 AM

Have redundant collision detection, one on each object.

Spawned Object:

     void OnCollisionEnter(Collision col)
     {
              if (col.gameObject.tag == "WhateverTheTagIs")
                     Destroy(this.gameObject);
     }

Existing Object:

     void OnCollisionEnter(Collision col)
     {
              if (col.gameObject.tag == "WhateverTheTagIs")
                     Destroy(col.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 nt314p · Oct 03, 2017 at 12:08 AM 0
Share

As I said above, the objects do not collide when they are spawned inside each other, but thanks for your efforts!

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

85 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

Related Questions

Why Original GameObject Destroy Clones ? 0 Answers

Different colliders in prefabs 0 Answers

Remove Object from Game after Collision with Player (Unity 5.2.3f1) 1 Answer

What do i do if i have problems with tags? 1 Answer

I need help destroying a clone on function. 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