Unity Collider2D is causing the game object to disappear upon collision?
I have gone through just about everything to try and isolate this bug.
I have isolated it down to the enemy collider, but have not solved the issue.
When my player collides with my enemy, the enemy game object is destroyed and removed from the hierarchy (until reset).
Removing the enemy collider fixes this issue of course. However removing the player collider does not.
Neither the player nor the enemy has collider code written into their scripts, so I can't blame that. I did have an OnEnterCollision2D script added to the enemy game object, but have since removed it in an effort to debug the situation, and it did not do a thing.
There are no other scripts at play - only the relationship between player and enemy colliders.
I'm newish to Unity and coding, but I can't seem to find anything on this issue. Any help is much appreciated!
What. The. Heck.
If you're wanting to read through the scripts, you can here: https://forum.unity.com/threads/oncollisionenter2d-is-destroying-my-game-objects-and-i-cannot-figure-out-why.710837/
Answer by jleven22 · Jul 16, 2019 at 06:45 PM
So I got some great advice on Reddit from user AverageArmadillo that I wanted to share and solved my issue.
"If you are using VS hit control + shift + F and under the search function change current document to entire solution to search your entire game solution. Look for Destroy.
Something... somewhere is making a call to destroy on collision. Remember that even inactive game objects with colliders can call collision on triggers. So if your player has some form of projectile they fire (I see an arrow in your scripts) if that arrow is not active in the hierarchy but present then it's trigger can still get fired. There is a chance that is happening.
I see it too in my game when bullets disabled still cause damage to enemies. The solution to that if it is the case was to add a check if object is active in hierarchy to the collision actions."
Turns out it was an issue with an old script in my archive folder that was calling a similar function to my new "hurt player" script.
Your answer
Follow this Question
Related Questions
Collision check 0 Answers
Collision With Text 0 Answers
Absolutely no collision detection in build but editor is fine 1 Answer
Collider Issue 0 Answers
my colliders don't work properly 2 Answers