Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by rkarrenbauer · Feb 25, 2017 at 05:27 PM · 2d gametriggerboxcollider2drpg-game

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.

Comment
Add comment · Show 7
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image tanoshimi · Feb 25, 2017 at 11:07 PM 0
Share

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.

avatar image rkarrenbauer · Feb 26, 2017 at 12:14 AM 0
Share
 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.

avatar image janus1001 rkarrenbauer · Feb 26, 2017 at 12:17 AM 0
Share

Is the Script attached to the Player or the enemy/area?

avatar image rkarrenbauer janus1001 · Feb 26, 2017 at 12:20 AM 0
Share

The script is attached to the area.

avatar image jdean300 rkarrenbauer · Feb 26, 2017 at 12:52 AM 0
Share

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?

avatar image rkarrenbauer jdean300 · Feb 26, 2017 at 01:06 AM 0
Share

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)

avatar image pcwUnity · Feb 26, 2017 at 08:56 AM 0
Share

@username Does the Player object in the hierarchy have it's tag set to "Player"?

3 Replies

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image rkarrenbauer · Feb 26, 2017 at 11:02 PM 0
Share

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?

avatar image
0

Answer by pcwUnity · Feb 26, 2017 at 08:56 AM

@username Check that the player object in the hierarchy has it's tag set to "Player".

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Weird 2d collision behaviour 1 Answer

Title letters appearing one after another 1 Answer

Can I have two colliders attatched to my enemy that do different things and if so how?,Is it possible to have two colliders for one object 2 Answers

Activate Trigger enable component doesn't work (2d mode.) 1 Answer

Why is this only working for the second object that collides? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges