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 /
This question was closed Oct 22, 2015 at 10:59 PM by meat5000.
avatar image
0
Question by csgeorge · Oct 22, 2015 at 09:55 PM · c#collision

Destroy a GameObject when it Collides with any other GameObject

I have a projectile that I want to be destroyed as soon as it collides with any other GameObject in the world. I have only been able to find solutions for destroying objects with OnCollisionEnter that involve checking for a specific tag or name. This is not a good solution for me as I want the projectile to be destroyed upon colliding with anything.

Here is what I've been trying. Using this code:

 void OnCollisionEnter (Collision collision) 
     {
           Destroy(gameObject);
     }

...the projectile is destroyed as soon as it spawns. Appears in the spot it is supposed to be fired from then instantly disappears. Next I tried this:

 void OnCollisionEnter (Collision collision) 
     {
         if(gameObject.tag != "MagShot")
         {
             Destroy(gameObject);
         }
     }

"MagShot" is the tag on the projectile, so basically I'm checking if it collides with anything other than itself. This way, the projectile spawns and fires fine, but is not destroyed when it collides with another GameObject.

How do I simply destroy a GameObject as soon as it collides with anything else in the scene?

Comment
Add comment · Show 1
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 meat5000 ♦ · Oct 22, 2015 at 11:03 PM 0
Share

Duplicate Question

https://www.google.co.uk/search?q=unity+gameobject+geting+destroyed+on+instantiate&ie=utf-8&oe=utf-8&gws_rd=cr&ei=PmspVpX-$$anonymous$$YrwauimpCA#q=unity+instantiated+gameobject+disappears

3 Replies

  • Sort: 
avatar image
0

Answer by Eno-Khaon · Oct 22, 2015 at 10:12 PM

Well, if your projectile is disappearing immediately upon creation, then that's because it's doing exactly what you're expecting; it's destroyed upon contact with the first thing it reaches.

Now, it's not what you're hoping for, but that's simply the current state of things. Here's what is most likely occurring, more precisely:

The projectile is created at the tip of a gun, let's say. The bullet is embedded slightly inside the gun so that it appears to fly out of the barrel upon creation... and therein lies the problem.

The projectile is (in your case) most likely colliding with whatever is firing it when it's created. As a result, your bullet recognizes that it hit something, regardless of what it is, and then destroyed itself.

So, how do you account for this? One relatively simple way is to put both the weapon and its projectiles on the same layer as each other and disable collisions between objects sharing that layer. Alternatively, if you'd prefer that bullets be able to collide with each other, put the bullets on a second layer and only disable collision between bullets and gun layers, but not bullet and bullet.

For an alternative approach, in case you want a gun to be able to shoot another player's gun (as applicable), Physics.IgnoreCollision() states clearly that "This is useful, say, for preventing projectiles from colliding with the object that fires them."

Comment
Add comment · Show 2 · 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 csgeorge · Oct 22, 2015 at 11:07 PM 0
Share

So this is what I assumed to be the problem initially (the projectile colliding with the character that spawns it) but since the point the projectile was spawning from is an empty object that is not actually touching the character, I thought that must not be the issue. But I tried again and I suppose the range of the character's collider must extend beyond the actual collider itself, because I simply moved it further away and now it works.

avatar image meat5000 ♦ csgeorge · Oct 22, 2015 at 11:29 PM 0
Share

Yup, all this talk of layers. Sometimes the answer is a lot simpler.

avatar image
0

Answer by CaKeMeaT · Oct 22, 2015 at 10:14 PM

because of the brilliance of physics layers, and your ability to choose which ones collide with the others, I would REALLY recommend you actually use tags. If you cant come up with a naming convention ("walls", "enemies", "obstacles", etc) then you might want to reconsider the overall design of your game.

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
avatar image
0

Answer by meat5000 · Oct 22, 2015 at 11:01 PM

the projectile is destroyed as soon as it spawns

Its colliding with your gun! Move the spawn point forward past the Player's colliders.

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

Follow this Question

Answers Answers and Comments

36 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

Related Questions

Unity3d OnCollisionEnter not firing 1 Answer

Advance Colision Detection 1 Answer

How can i restart a script on collision ? 1 Answer

Properly attach to a GameObject after collision? 0 Answers

Changing moving direction after hit other object 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