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 opponent019 · Apr 21, 2014 at 09:15 PM · 2dtriggerontriggerenterontriggerenter2d2 player

OnTriggerEnter2D not working, alternatives?

I've read that OnTriggerEnter2D has some issues, so I was wondering if there was another way to achieve what I need: getting the level to end after the 2 players are on their respective flag (there is no way one can get to the other's flag), here's what I had:

     private int flag = 0;

     void OnTriggerEnter2D(Collider2D col){
         if (col.gameObject.tag == "Player") {
             flag = flag + 1;
             Debug.Log ("Flagged = " + flag);
         }
     }
 
     void OnTriggerExit2D(Collider2D col){
         if (col.gameObject.tag == "Player") {
             flag -= 1;
             Debug.Log ("Unflaged" + flag);
         }
     }
     
     // Update is called once per frame
     void Update () {
         if (flag == 2) {
             Debug.Log ("Level finished");
             Application.LoadLevel ("1");
         }
     }


Each player is on a different layer but on the same tag (Player) because it's impossible for them to get to the other's space. The console prints

onTriggerEnterEvent: Goals UnityEngine.Debug:Log(Object)

but it won't add anything to the variable flag.

Is there other way to do this? or is this the correct way but I'm doing something wrong?

Comment
Add comment · Show 4
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 Pyrian · Apr 21, 2014 at 10:34 PM 0
Share

Wait - where is the flag variable declared? If you have two flag GameObjects, and flag is declared as part of their object, then you'll have two instances of flag, neither of which can ever reach 2.

avatar image opponent019 · Apr 21, 2014 at 10:36 PM 0
Share

Oh, I thought I had coppied it as well, it's declared before OnTriggerEnter2D as a private int, initialized in 0 $$anonymous$$mmh it's there, but for some reason it got out of the Code thing, sorry, I'll fix that

avatar image Pyrian · Apr 21, 2014 at 10:44 PM 0
Share

Well, I figured. But I don't see how the flag variable can ever reach two, since each flag object will have its own flag variable, so each one will only ever reach up to one. $$anonymous$$aybe if you declared it as a static.

avatar image opponent019 · Apr 21, 2014 at 11:19 PM 0
Share

even if the script is in the GameObject that has the 2 triggers? I mean, the triggers aren't on each flag/goal, they're on an empty GameObject parent of the 2 flags. Anyway, I tried it and it didn't work, and if I set it as public (to see the value) it never get to 1 either.

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by opponent019 · Apr 23, 2014 at 10:30 PM

Oh! this fixed it! Thanks Pyrian for trying to help me anyway :) http://answers.unity3d.com/questions/592567/why-isnt-my-ontriggerenter2d-function-working.html

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 Pyrian · Apr 24, 2014 at 01:13 AM 0
Share

Interesting. The tutorials talk about the fact that at least one of the colliding objects must have a RigidBody2D - they gloss over the fact that it must not be kinematic! Seems dumb to enable force-free kinematics just to get a trigger to fire, but it DOES work with a $$anonymous$$imum of fuss. I wonder what the performance cost is, though? $$anonymous$$ight be better to do something like Physics2D.OverlapArea, although it'd be a pile of work to do that for a complicated collider. Ugh. Thanks for the tip, sorry I couldn't help!

avatar image
0

Answer by Pyrian · Apr 22, 2014 at 04:14 AM

Ohhh, that's your problem. Triggers won't get called in parent object scripts. You have to have the script catching the trigger as a component of the exact same GameObject where the Trigger Collider actually is.

So, what's your next step? Probably the easiest solution is to move that script into each of the children, while making the flag a static so that it's value is shared between them.

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 opponent019 · Apr 22, 2014 at 04:23 AM 0
Share

Oh no no, sorry English is my second language so I can't explain myself very well; this GameObject (Goals) is parent of 2 objects: goal1 and goal2, Goals has 2 box colliders that are triggers, each positioned where the children are, but the colliders are in Goals, not each little goal; the script (Flagged) is in Goals as well

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

21 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 avatar image avatar image

Related Questions

OnTriggerEnter detection too late 0 Answers

Trigger only one collider? 1 Answer

Ontriggerstay2d only runs 26 times in a row? 3 Answers

How can i detect hits from enemies inside of a player? 1 Answer

OnTriggerEnter2D Doesn't work? ;_; 4 Answers


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