- Home /
Unity bug - detecting a collision that doesn't occur?
So I have 3 sprites: 1.) a ball with a Rigid Body 2D, a Box Collider 2D, and tag "Ball" 2.) a transparent grey square with a Box Collider 2D (Is Trigger) and tag "Area" 3.) a green rectangle with a Box Collider 2D and tag "Ground". A script called BallMovement is attached to the ball. The script uses the following code to toggle a boolean "inContact" between true and false:
void OnTriggerEnter2D (Collider2D other) {
if (other.gameObject.tag == "Area") {
inContact = true;
}
}
void OnTriggerExit2D (Collider2D other) {
if (other.gameObject.tag == "Area") {
inContact = false;
}
}
At start, the ball falls from the top of the screen and, when it comes in contact with the grey square the user must press the spacebar. If the user presses the spacebar at the same time that inContact == true then a force is applied to the ball to make it move upward. On its way back down, the user will again have to hit spacebar to keep the ball in the air. If he fails to do this, the ball will fall down towards the ground. Using the following code, the script detects the collision between the ball and the ground and toggles a boolean called "miss" between true and false:
void OnCollisionEnter2D (Collision2D collision) {
if (collision.gameObject.tag == "Ground") {
miss = true;
}
}
Another script called GameManager checks to see if miss == true and, if so, runs the following code to end the game and displayed the main menu screen:
void Update () {
if (BananaMovement.miss) {
Application.LoadLevel(0);
BananaMovement.miss = false;
}
}
For some reason, even when the ball is clearly not in contact with the ground (even when at the complete opposite end of the screen) the game will end and the main menu screen will be loaded. When I set it up so that the ball would automatically move back upward when coming in contact with the grey square (no user input needed), it would run fine with no interruption HOWEVER if I clicked on something in the inspector or if I clicked Stats to see my FPS while it was running it would sometimes quit out for no reason. I have checked my code over and over and I am 100% sure there is nothing in my code that would cause this to happen. Is this a bug in Unity?
You don't provide enough of your scripts for anyone to see if your logic is sound. If the 1000s of questions I've been involved in, only one was a Unity bug. So the odds are against you, but not impossible. Is there anything in the log file?
Your answer
Follow this Question
Related Questions
Need help: Replay have bug... 0 Answers
Error while using Time.deltaTime 2 Answers
Why does all my information in Unity dissappear 0 Answers
multiple shaders per shader file 2 Answers
Strange unity problem 0 Answers