Which object needs to be the trigger?
[This is in C#]
I have two objects that have basic 2D colliders, and one of them needs to be a trigger for OnTriggerEnter to work. That much I understand. What I am confused is which object needs to have what in order for the whole process to work correctly. The purpose of this trigger is to have one object (NPC) teleport to the position of the other object (Destoryer) and then the NPC gameObject will be destroyed. A Debug.Log() was also set up to test whether collision occured. Here is what I've tried so far:
-The Destroyer had the script and was a trigger, the NPC was not a trigger and had a rigidbody set to Kinematic; this test failed.
then took off the Rigidbody and tested it; this too failed.
then switched around the script so that it was on the NPC (the gameObject would destroy itself), but kept the Destroyer as a trigger and kept the rigidbody; this did not work.
did the above but without a Rigidbody; this did not work.
made them both triggers and then neither of them triggers, going through all of the above scenarios; this did not work.
I'm running out of options (and sanity). What is the problem I am facing? Let me know if this makes sense, as well.
Answer by LetsBeChillx · May 27, 2017 at 09:27 PM
I am a complete idiot. I did not realize that I had to use OnTriggerEnter2D (Collider 2D other) when Unity is set to 2D (simply, I am used to working in 3D). Each NPC also needed a kinematic Rigidbody2D for the collision to register, for some reason. Thank you regardless for your help, and best of luck to everyone following.
Answer by UnityCoach · May 13, 2017 at 01:16 AM
The object that receives the message OnTriggerEnter is the one that has the "trigger" property set to true.
It is passed the "other" collider, that one should have "trigger" set to false.
So, you triggerZone script should have OnTriggerEnter method and trigger set to true, while your NPC should have a simple collider.
Your answer
Follow this Question
Related Questions
Destroy GameObject inconsistent with Instantiated children 1 Answer
c# match 3 destroy... 1 Answer
GameObject not colliding properly with a collider! 3 Answers
rigidbody not moving 1 Answer
Player cannot destroy enemy 1 Answer