OnTriggerEnter2D problem
Hello!
First of all, I know there's multiple questions about OnTriggerEnter2D but can't really find answer for this. In my game player attacks by throwing bottles. I made script for walls which should destroy gameobject if it doesn't have tag "Player" or "Enemy". So only thing what walls should destroy is those bottles. Now my walls destroys those bottles but also enemies. It doesn't destroy player because in players inspector Is Trigger isn't checked in the Box Collider 2D. Enemys Is Trigger is checked, if I uncheck it walls doesn't destroy enemy but also bottles doesn't destroy enemy.
There is my script for walls:
https://github.com/arttuhar/laivapelitesti/blob/master/Walls.cs
...and there is my script for bottle:
https://github.com/arttuhar/laivapelitesti/blob/master/Bottle.cs
I can give more information what is in inspector if needed.
I'm beginner and this is my school project. I asked if my teacher could help me but he don't know how to fix this. Thank you in advance.
Answer by DonRavel · Nov 27, 2017 at 11:31 PM
The only issues I see is on your "Walls" script.
The first issue is that you should compare the tag for the "other" gameObject (the object colliding with the Walls), instead of the wall gameObject. So, replace "gameObject" by "other".
The second issue is: replace the || for && on the Walls script. What you need is that the tag is neither player nor enemy. By putting || you're allowing the "if" to be executed either by the tag not being "player", or the tag not being "enemy".
So the correct should be:
if (!other.CompareTag ("Player") && !other.CompareTag ("Enemy"))
Hope it helps. :)
Your answer
Follow this Question
Related Questions
GameObject keeps colliding for sometime after being destroyed 1 Answer
Why won't my model rotate up on X axis? 1 Answer
OnTriggerExit does not work 0 Answers
Player not moving in the right direction instantly 1 Answer
Help with Script 0 Answers