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 Bostonian02 · Mar 30, 2017 at 10:20 PM · c#collisiongameobject

Why is my game object not being destroyed?

Hello there Unity community, I'm working on remaking Space Invaders in Unity for a little STEM Fair that is going on locally in my area. I started working on the project a few days ago and all was going well until I ran into a roadblock. When the player fires a laser beam and it collides with the invader, the invader is not destroyed. I have a rigidbody 2D and a box collider 2D on both of the game objects but nothing is happening. I have attached my script to the invader but I have no idea what is going wrong. I'll attach my code and some images of the inspector for both game objects.

 public class InvaderDestroy : MonoBehaviour {
     public void OnCollisionEnter(Collider other) {
         if (other.gameObject.tag == "Boundary") {
             return;
         }
         if (other.gameObject.tag == "Lazer") {
             Destroy(gameObject);
         }
         Destroy(other);
     }
 }


alt text

alt text

Thank you very much for your time!

invader-inspector.jpg (99.3 kB)
lazer-inspector.jpg (94.8 kB)
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
0
Best Answer

Answer by eatsleepindie · Mar 31, 2017 at 02:37 AM

Three things, which I am posting here in case anyone else has this problem.

First, set your lazer's collider back to a Trigger by ticking 'isTrigger.'

Second, you were using OnCollisionEnter2D when you want to use OnTriggerEnter2D.

Third, the Collider, not the Collision, is what is passed to the OnTriggerEnter2D function. Here is what you had:

 public void OnCollisionEnter2D(Collision2D other) {
         Debug.Log( "We got a collision" );
         if (other.gameObject.tag == "Boundary") {
             return;
         }
         Debug.Log( "Collision with object not tagged as Boundary" );
         Destroy(other.gameObject);
         if (other.gameObject.tag == "Lazer") {
             Debug.Log( "Collision with object tagged as Lazer" );
             Destroy(gameObject);
         }
     }

And here is the working code. Note that since you are using 2D Physics, just about everything (Colliders, Collisions, etc) will end with '2D'.

 public void OnTriggerEnter2D(Collider2D other) {
         Debug.Log( "We got a collision" );
         if (other.gameObject.tag == "Boundary") {
             return;
         }
         Debug.Log( "Collision with object not tagged as Boundary" );
         Destroy(other.gameObject);
         if (other.gameObject.tag == "Lazer") {
             Debug.Log( "Collision with object tagged as Lazer" );
             Destroy(gameObject);
         }
     }
Comment
Add comment · Show 11 · 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 Bostonian02 · Mar 31, 2017 at 02:47 AM 0
Share

Well I tried the OnCollisonEnter2D and it had no effect. Do you think it may be something with the box collider or rigidbody?

avatar image eatsleepindie · Mar 31, 2017 at 03:23 AM 0
Share

This was my bad. You are destroying the object that is calling the Destroy(other) method first, so the second Destroy is likely not being called at all.

  • You also likely want to call Destroy(other.gameObject) unless you are trying to destroy the collider component

  • I moved the updated code to my original answer because code formatting is not playing nice tonight.

  • $$anonymous$$eep in $$anonymous$$d that since Destroy(other.gameObject) is outside the if statement, any object that collides that does not have a tag of "Boundary" will be destroyed. If you only want it to destroy if it is hit by a gameobject tagged "Lazer" then move Destroy(other.gameObject) to inside the if(other.gameObject.tag == "Lazer") { conditional, just before you call Destroy(gameObject);

avatar image Bostonian02 eatsleepindie · Mar 31, 2017 at 03:31 AM 0
Share

This also didn't change anything. But I'm not so sure it'll matter anymore. I just upgraded to Unity 5.5 and I was making this in Unity 5.4 so because of the update it completely screwed up my project settings so I'm just going to work from the ground up. I may or may not have this problem anymore. But I will use your code right from the start so hopefully I don't have this issue again.

avatar image eatsleepindie · Mar 31, 2017 at 03:40 AM 0
Share

I added some logging to the function. Update the script and check the console to ensure that the collision is registering at all. If you get a ton of boundary collisions in the console output simply select the option "Collapse" in the top of the Console. If you see the Console output "Lazer Collision" then the detection is working properly. If you get nothing in the Console at all then no collision is being detected at all.

I'm not trying to dissuade you from starting over, but this is a good opportunity to learn how to fix this issue which is one you could potentially run into often. There are a lot of things that could be causing the collision to not be detected by the Physics engine at all.

avatar image eatsleepindie · Mar 31, 2017 at 03:47 AM 0
Share

I just noticed your Lazer object's collider is marked as a trigger. Try using OnTriggerEnter2D rather than OnCollisionEnter2D.

avatar image Bostonian02 eatsleepindie · Mar 31, 2017 at 03:49 AM 0
Share

Yeah I tried switching between the two but go no results with either one :/ I guess I just forgot to turn it off when I switched to collision ins$$anonymous$$d.

Also thank you so much for your help with this. It had been going so smoothly and then I ran into a problem I've never had before. I didn't think I'd run into someone who would stick with this problem and see it solved. I hope that more forum members can be like you :)

avatar image eatsleepindie Bostonian02 · Mar 31, 2017 at 03:54 AM 0
Share

You're welcome. This community helped me out a ton when I first started out, I'm just trying to pay it forward. If you want to send me a copy of the project I'd be happy to take a look. Zip it up and send it to info@eatsleepindie.com

Show more comments
avatar image Bostonian02 · Mar 31, 2017 at 03:44 PM 0
Share

It works! I don't know what did the trick but it all works now. It's logging the collisions and the invader is destroyed! I was using some guy's tutorial and his code is super messed up. Thank you so much for your time, I thought I almost had to abandon this project and choose something else to do for the STE$$anonymous$$ Fair. If you want, when the project is done I'm gonna throw it on GitHub and I can send you a link, as a 'thank you' for all your hard work on helping me fix this issue.

avatar image eatsleepindie Bostonian02 · Mar 31, 2017 at 04:45 PM 0
Share

Absolutely send me a link, you've got my e-mail. And you are very welcome, I'm happy to have been able to help.

Best of luck with your project!

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

139 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 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 can i allow my character to jump if he has a powerup.,Changing a variable from one script to another 0 Answers

Detect overlapping objects 2D game 0 Answers

How do I get my character to collide with an object, he dies, and respawnee at the beginning of the map? 1 Answer

How to get parent with component on collision? 0 Answers

Error CS1519 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