• 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 WorldEater · Dec 17, 2017 at 09:48 PM · c#collider2dontriggerexit

Question regarding OnTriggerExit2D for destroyed objects

So I'm working on a Asteroids like game where I want the asteroids to be destroyed and add some points to the score whenever they leave the play-area (defined by a Circle trigger). However, for some reason whenever an asteroid is destroyed inside the play-area the OnTriggerExit2D function is called. This is not the desired since it will then add points to the score. I'm fairly sure this was not how OnTriggerExit2D worked previously.


     void OnTriggerExit2D(Collider2D other){
         if (other.tag == "Asteroid") {
             GameManager.instance.AddScore(scoreToAdd);
             other.GetComponent<Asteroid> ().DestroyAsteroid ();
         }
     }



Any Ideas on how I can check if asteroid has actually left the play-area or if it has been destroyed in some other way?

Thanks.

Comment
Add comment · Show 20
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 KittenSnipes · Dec 17, 2017 at 10:55 PM 0
Share

@WorldEater

I think that the instantiated objects are actually created inside of said trigger making it call the OnTriggerExit because it never actually enters it

avatar image WorldEater KittenSnipes · Dec 17, 2017 at 11:46 PM 0
Share

@$$anonymous$$ittenSnipes Thank you for your comment, I'v tried instantiating the asteroids both inside and outside the play-area but the behavior stays the same, the OnTriggerExit2D function is called no matter what. :/

avatar image KittenSnipes · Dec 18, 2017 at 02:07 AM 0
Share

@WorldEater

Well what exactly is your OnTriggerExit2D for? Another great thing to look at if you need some inspiration is the 2d tutorial on the Unity launcher under learn. There is a tutorial that sounds very similar to your game.

avatar image KittenSnipes · Dec 18, 2017 at 02:14 AM 0
Share

Jk about the comment about your OnTriggerExit I meant. Try making a bool inside of OnTriggerStay. Basically do like:

 bool allowedToExit = true;
 void OnTriggerStay2D(Collider other) {
     if(other.collider.tag == “asteroid”) {
         allowedToExit = false;
     }
 }
 
 void OnTriggerExit2D(Collider other) {
     if (allowedToExit == true) {
         //Do the Stuff 
     }
 }
avatar image WorldEater KittenSnipes · Dec 18, 2017 at 02:28 AM 0
Share

@$$anonymous$$ittenSnipes I don't think this will help, the core of the problem is that OnTriggerExit2D is called both when the object exits the trigger as well as when the object is destroyed and I currently do not have a way to tell them apart. One way to solve this would be to simply create a custom 2d collider witch encircles the play aria and use OnTriggerEnter2D ins$$anonymous$$d... but that feels like a bit of a hack.

Again thank you for helping me out with this, it's greatly appreciated!

avatar image KittenSnipes · Dec 18, 2017 at 02:35 AM 0
Share

@WorldEater

I think that maybe it could be your OnTriggerEnter2D or OnTriggerStay2D. I kind of want to know just to be sure. $$anonymous$$aybe we can find a work around.

avatar image WorldEater KittenSnipes · Dec 18, 2017 at 02:44 AM 0
Share

I have no OnTriggerEnter or Stay in the class currently.

avatar image KittenSnipes WorldEater · Dec 18, 2017 at 02:50 AM 0
Share

@WorldEater

$$anonymous$$aybe you can make one to just test around.

Show more comments
avatar image JVLVince · Dec 18, 2017 at 03:01 AM 0
Share

Can you show me the process inside this DestroyAsteroid () function? And What kind of collider2D you are using in both circle and Asteroid? (2D box collider or circle collider ...)

avatar image JVLVince JVLVince · Dec 18, 2017 at 03:46 AM 1
Share

O$$anonymous$$, since I don't know exactly what your setting look like so I can not give the exactly problem you have but I think this point will help. OnTriggerExit2D will fire when collision is exit (of course) and A game object when it's be destroy, The collision attach on the destroyed GO (gameobject) will be destroyed too, and you can say the collision of the destroyed GO has been "Exited" too, that's why the OnTriggerExit2D fire.
I just can advice you add flag to separate when the Asteroid destroyed by out of play area and when the Asteroid destroyed by player.
Inside Asteroid.cs

 public class Asteroid{
     private bool m_outAreaDestroyed = false;
     public bool OutAreaDestroyed{
         get; private set;
     }
 }

Inside the area collision check code you just check if other.GetComponent().OutAreaDestroyed == true then do the stuff you give below.

avatar image WorldEater JVLVince · Dec 18, 2017 at 03:53 AM 0
Share

I appreciate the comment, I will do some more investigation tomorrow and get back to you.

Show more comments
Show more comments
avatar image KittenSnipes · Dec 18, 2017 at 03:25 AM 1
Share

Of course I’m open to help at any time so don’t be afraid to ask :D $$anonymous$$aybe a bit of a better look at your code. Anything to help

avatar image Nikaas · Dec 18, 2017 at 10:46 AM 0
Share

I would swap the code to the asteroids. No reason for play-area to take care for asteroid (or anything that's not self). Ins$$anonymous$$d i would make asteroids destroy self on exiting play-area. This way you can differentiate between play-area and bullet triggers. This will probably be enough, but if not then you can additionally make the asteroid self-check for transform.activeInHierarchy on trigger exit.

avatar image WorldEater Nikaas · Dec 18, 2017 at 11:20 AM 0
Share

I don't know why I did not think of this, it will defiantly solve my problem. Thank you!

avatar image Nikaas WorldEater · Dec 18, 2017 at 11:39 AM 0
Share

@WorldEater One of the important things I learned not long ago - Every gameObject/prefab should be responsible only for things related to self. It would make things much easier in the long run. Imagine having asteroid code on the asteroid, playarea, bullets and say 5 more objects - now something unexpected happens with the asteroid and you have no idea what caused it and where to look. Now imagine you have hundreds of types of gameObjects/prefabs with their code spred like that.

1 Reply

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

Answer by WorldEater · Dec 18, 2017 at 11:24 AM

After some good comments and a good nights sleep the issue was solved by moving the code from the play-area to the asteroids. The asteroids now check if they leave the play-area in this manner:

    void OnTriggerExit2D(Collider2D other){
          if (other.tag == "PlayArea") {
              GameManager.instance.AddScore(scoreToAdd);
              DestroyAsteroid ();
          }
      }

Thanks to everyone who commented!

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Illuminating a 3D object's edges OnMouseOver (script in c#)? 1 Answer

Is There A Way To Disable a Trigger But Still Use OnTriggerExit2D? (For COOP switch management) 2 Answers

Check Which Collider Collided? 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