- Home /
I changed from 2D to 3D. Everything works fine now.
OnCollisionExit2D not getting called?
Hello, While trying to make a pretty simple game for my friend, I encountered an annoying problem. My problem is that OnCollisionExit2D just isn't getting called when my player stops touching the ground, but OnCollisionExit2D works with other objects. I've done some research, and found the common solution of adding a Rigidbody2D to the ground, but that didn't work. Here's my setup: Ground game object: Player game object:
Player collision methods:
void OnCollisionEnter2D (Collision2D collision)
{
Debug.Log ("Enter: " + collision.gameObject.tag);
if (collision.gameObject.tag.Equals ("Ground")) {
touchingGround = true;
}
if (collision.gameObject.tag.Equals ("Obstacle")) {
collision.gameObject.GetComponent<ObstacleScript> ().moveObstacle = false;
gameOver = true;
}
}
void OnCollisionExit2D (Collision2D collision)
{
Debug.Log ("Exit: " + collision.gameObject.tag);
if (collision.gameObject.tag.Equals ("Ground")) {
touchingGround = false;
}
}
Output in console:
Enter: Ground
(Exit: Ground supposed to be here)
(Enter: Ground supposed to be here)
Enter: Obstacle
If you need any more information, please ask.
Thank you,
SeeSharp.
If the images are broken for you like they are for me(I don't know why), the links are: Ground game object Player game object
Answer by HarshadK · Jul 22, 2014 at 02:11 PM
Check for equality using '==' operator.
So the code will become:
void OnCollisionEnter2D (Collision2D collision)
{
Debug.Log ("Enter: " + collision.gameObject.tag);
if (collision.gameObject.tag == "Ground") {
touchingGround = true;
}
if (collision.gameObject.tag == "Obstacle") {
collision.gameObject.GetComponent<ObstacleScript> ().moveObstacle = false;
gameOver = true;
}
}
void OnCollisionExit2D (Collision2D collision)
{
Debug.Log ("Exit: " + collision.gameObject.tag);
if (collision.gameObject.tag == "Ground") {
touchingGround = false;
}
}
Thanks for the suggestion, but that still doesn't answer my problem. OnCollisionExit2D isn't getting called, I'm not getting any Debug calls. I use .Equals() because of my Java habits, and in Java == doesn't always work when comparing strings.
I also Google'd "C# .equals vs ==" and some people stated that .equals would be more accurate as it compares the characters and not the actual object.
Thank you,
SeeSharp.
Follow this Question
Related Questions
OnTriggerEnter not working 0 Answers
Character Animation Wont Play 2 Answers
troble with the Axis in unity 1 Answer
SetColor not working? 1 Answer
MuzzleFlash not working 0 Answers