[SOLVED] Problem with OnCollisionEnter2D script after upgrading to Unity 5.3(from Unity 4.6)

Hello guys! I’m currently doing a Unity course on Udemy. I’m a newbie so you might see some really bad coding skills and if you can help me in a friendly manner it would be appreciated. Also this is my first question here so if I have made it wrong, feedback is also appreciated :slight_smile:

I have simple block breaker type of game. But after upgrading to Unity 5.3 my game broke.

I have a ball and bricks in a scene. Ball hits a brick and the brick gets destroyed. I wanted to make it a bit more interesting so I added a piece of code that after collision the ball bounces off the brick and it makes the brick lose its box collider, brick is set to not kinematic, and the brick falls down through everything off the screen.

This is not happening however. Instead the ball flies THROUGH the brick(and other bricks because there are more of them in the scene) instead of bouncing off of it, and the brick falls down.

Expected Behavior: Ball collides with a brick, Ball bounces off the brick and that brick falls down off the screen and destroys itself

What is happening: Ball flies through the brick, doesnt bounce and the brick falls down off the screen.

I tracked it down and it is the line where I turn off the box collider. If I comment it it will bounce and everything “works”. If i uncomment it it flies through. To me it seems like the physics(bounce) is applied after the brick has the box collider turned off. It works exactly like if it was set to trigger. Thing is this worked perfectly in unity 4.6 and doesn’t in Unity 5.3. And no, I don’t have either of them(brick or ball) set to trigger

Also I’ve looked into unity 5 changelog and they say they made changes to OnCollisionEnter2D but I don’t understand what they mean. If someone can explain to me what changes have been made and why my code is not working in Unity 5.3 it would be EPIC!

Thanks in advance and here is the code:

void something() {
	
	brickCount--;
	GameObject.FindObjectOfType<LevelManager>().blocksDestroyed();
	rb.isKinematic = false;
	GetComponent<BoxCollider2D>().enabled = false; //this line seems to be causing the problem
	rb.gravityScale = 1;
	Destroy (gameObject, 2);
}

//function that does things I want on collision
void OnCollisionEnter2D (Collision2D collision) {
	AudioSource.PlayClipAtPoint (clip, gameObject.transform.position, 0.153f);
	hitCount++;
	GameObject.FindObjectOfType<TextControler>().pointcount();
	
	//if the object hit is chizow then it will follow the if statement
	if (gameObject.tag == "chizow") {
		makeSmall(0.1f);
		if (hitCount >= (Difficulty.maxHits * 2)) {
			something();
		}
	//if it doesnt have chizow tag(therfore it is any other brick) it will do things specified in if condition
	} else {
		makeSmall(0.15f);
		if (hitCount >= Difficulty.maxHits) {
			something();
		}	
	}
	
}

Hello, @DeathMade2015.

As soon as the ball collides with a brick, the 2DBoxCollider of the brick is disabled. That is how the ball passed through all of the bricks that it collided with. To achieve the result you want, you will need to use function OnCollisionExit2D. In that function, for the brick that the ball collided with, disable its 2DBoxCollider and then set its rigidbody2D isKinematic variable to false.