- Home /
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.
I think that the instantiated objects are actually created inside of said trigger making it call the OnTriggerExit because it never actually enters it
@$$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. :/
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.
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
}
}
@$$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!
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.
I have no OnTriggerEnter or Stay in the class currently.
$$anonymous$$aybe you can make one to just test around.
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
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!
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
C# Reset EdgeCollider2D.Points.Length and LineRenderer.Positions.Length to 0 0 Answers
trouble with collider2D.cast 2 Answers