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);
}
}
Thank you very much for your time!
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);
}
}
Well I tried the OnCollisonEnter2D and it had no effect. Do you think it may be something with the box collider or rigidbody?
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);
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.
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.
I just noticed your Lazer object's collider is marked as a trigger. Try using OnTriggerEnter2D rather than OnCollisionEnter2D.
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 :)
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
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.
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
Follow this Question
Related Questions
Detect overlapping objects 2D game 0 Answers
How to get parent with component on collision? 0 Answers
Error CS1519 1 Answer