- Home /
Unity 5.5 - How to Properly Fix 2D Box Collider Trigger?
Okay, so I have read multiple answers on how to fix a 2D box collider trigger. The majority of them say to add 2D box colliders to both objects involved, give them both 2D rigidbodies, make sure they're set to dynamic and not kinematic, set their gravity scales to 0, etc. I have tried all of these options, yet my OnTriggerEnter2D method still does not work at all. I'm thinking maybe the problem is that all previous threads regarding OnTriggerEnter2D fixes were answered for previous versions of Unity, as none of the solutions worked on my version, which is Unity 5.5. Does anybody know how to get the triggers to activate on 5.5? Thank you very much.
Collisions in 5.5 work exactly the same way as in every previous version of Unity. Are you using the correct function signature for OnTriggerEnter2D? Please post your code.
void OnTriggerEnter2D(Collider2D other)
{
Debug.Log("Area entered");
if (other.name == "Player")
{
Debug.Log("I'm in");
theTextBox.ReloadScript(theText);
theTextBox.currentLine = startLine;
theTextBox.endAtLine = endLine;
theTextBox.EnableTextBox();
if (destroyWhenActivated)
{
Destroy(gameObject);
}
}
}
I am currently trying to use this code to activate my dialogue boxes whenever the player walks into a box collider of an NPC. The log text "Area entered" shows every time I run the game, but the "I'm in" text never activated once in my testing. Both the player and the NPC have 2D rigid bodies and box colliders. I don't understand what is wrong.
Is the Script attached to the Player or the enemy/area?
If "area entered" is being shown, but "I'm in" is not, then there is obviously something wrong with your if statement. Are you sure the collider is on the object named Player? If it's on "Player"s child then you will get the childs name. Why don't you Debug.Log(other.name)
and see what shows up?
Debug.Log(other.name) comes up with "NPC". I have a player, an NPC, and an NPC Activation Area.
If it helps, I've been following this tutorial: https://www.youtube.com/watch?v=7$$anonymous$$NQYPcx-uU (around 20 $$anonymous$$utes in)
@username Does the Player object in the hierarchy have it's tag set to "Player"?
Answer by tanoshimi · Feb 26, 2017 at 07:20 AM
You've kind of answered your own question as to why the part of your script contained within
if (other.name == "Player") { ... }
doesn't work, because Debug.Log(other.name) has told you that the thing you're colliding with is not named "Player", it's named "NPC".
So either you've named your gameobjects incorrectly or you've attached this script to the wrong object.
The reason NPC is showing up is because I have the trigger area surrounding the NPC. I want the code for the if statement to trigger when the Player walks into the NPC trigger area. I have attached the script to the NPC trigger area.
Here's what I've tried since posting this question and receiving feedback: I have tried making the if statement look out for "NPC", but that activates the trigger immediately, ins$$anonymous$$d of when the player walks into the area. I have also tried attaching the script to the player ins$$anonymous$$d of the trigger area looking for "Player", but that produces the same outcome as before. I tried changing the if statement to look out for "NPC" while the script is attached to the player. Doing that provided a NullReferenceException: Object reference not set to an instance of an object.
What else is there to try?
Answer by rkarrenbauer · Feb 28, 2017 at 07:39 AM
https://drive.google.com/open?id=0B51LnCsOPDT1dHdMbWpnWkRRYkU
I have provided a link to my project so you can see the OnTriggerEnter2D bug in action.