Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 cmpgk1024 · Dec 15, 2013 at 04:54 PM · collisiontrigger

OnTriggerEnter2D being called, but not OnTriggerExit2D?

I want to increment the player's score when they successfully clear a box. To do this, I have one collider on the box for collisions, and one trigger over the box to see if the player is on top of it.
the box

I have this script attached to the player to increment the score:

     public int score = 0;
 
     void OnTriggerExit2D(Collider2D col){
         if (col.gameObject.tag == "Obstacle")
                         score++;
     }  

OnTriggerExit2D does not get called in this script, but if I change it to OnTriggerEnter2D it does. I cannot use that, because it constantly increments the score while the player is in the trigger. How can I fix this?

screen shot 2013-12-15 at 11.51.37 am.png (7.6 kB)
Comment
Add comment · Show 1
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 OP_toss · Dec 15, 2013 at 08:19 PM 0
Share

I'm unfamiliar with the 2D handlers, but the normal OnTriggerEnter should only be called once, when you first enter the trigger, not repeatedly while you are inside it.

But for the sake of a quick workaround, just keep a variable pointer to the last entered obstacle. Then check to see if you've already passed that obstacle before incrementing again.

Hope this helps!

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Kilrath81 · Dec 15, 2013 at 08:24 PM

the 2d trigger stuff seems to be having issues. one way around it would be to have a empty game object with a boxcollider2d as a trigger just above and do an oncollisionenter2d for that tag to do your score.

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 small-U · Mar 09, 2014 at 06:43 AM

can move GameObject(MoveObject) add Rigidbody2D component and Box Collider2D, other one GameObject(targetObject) add Box Collider2D setting [isTrigger true] look my pictures..

alt text alt text

the Test4 script code:

 void OnTriggerEnter2D(Collider2D collisionInfo) {
     Debug.LogError(" OnTriggerEnter2D  ");
 }


 void OnTriggerExit2D(Collider2D collisionInfo) {
     Debug.LogError(" OnTriggerExit2D  ");
 }

1.png (48.8 kB)
2.png (30.1 kB)
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

19 People are following this question.

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

Related Questions

How to add to a 3D object's length through code? 1 Answer

Overlap point of 2 kinematic colliders 1 Answer

How to add the right amount of points when my player destroys a gameobject? 0 Answers

Receiving information about getting hit by a Raycast 2 Answers

How do you execute Trigger-collider collision only in one gameobject? 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