Brick Breaker - Ball not bouncing back from the brick.

I have a problem. When my ball is touching a brick it does not bounce back from that brick, but it gets through that brick and the brick is destroyed. Any idea why?

using UnityEngine;
using System.Collections;

public class Brick : MonoBehaviour {

	public int maxHits;
	private int timesHit;

	private LevelManager levelManager;

	// Use this for initialization
	void Start () {
		timesHit = 0;
		levelManager = GameObject.FindObjectOfType<LevelManager>();
	}
	
	// Update is called once per frame
	void Update () {

	}

	void OnCollisionEnter2D(Collision2D col){
		timesHit++;
		if (maxHits == timesHit) {
			Destroy (gameObject);
		}

	}

        //TODO change this later;
	void SimulateWin() {
		levelManager.LoadNextLevel ();
	}



}

This is my Brick script. What I want to achieve is to destroy that brick and my ball bounce from that before it gets destroyed.

Click on your Ball object, and go to its RigidBody2D, there is a setting that is named Collision Detection, it’s very likely that yours is on Discreet, change it and put it on Continuous and it should work like a breeze!

There is no code making it bounce back, and because it’s destroying the brick, there’s nothing there to bounce back from. You should just inverse the y velocity of the ball’s Rigidbody2D before you destroy the gameobject in the OnCollisionEnter2D function.

EDIT: (Thanks to @fafasefor helping improve this answer!)
To do this, just add this line of code to the top of your script public Rigidbody2D ball; then go to the inspector, assign the ball to the to the class.
Now just add this line of code ball.velocity = new Vector2 (ball.velocity.x, -ball.velocity.y) to theOnCollisionEnter2D function. All this is doing is inverting the balls y velocity to send it back down.

Your class should now look like

 using UnityEngine;
 using System.Collections;
 
 public class Brick : MonoBehaviour {
 
     public int maxHits;
     private int timesHit;

     public Rigidbody2D ball;

     private LevelManager levelManager;
 
     // Use this for initialization
     void Start () {
         timesHit = 0;
         levelManager = GameObject.FindObjectOfType<LevelManager>();
     }
     
     // Update is called once per frame
     void Update () {
 
     }
 
     void OnCollisionEnter2D(Collision2D col){
         timesHit++;
         if (maxHits == timesHit) {
             ball.velocity = new Vector2 (ball.velocity.x, -ball.velocity.y)
             Destroy (gameObject);
         }
     }
 
         //TODO change this later;
     void SimulateWin() {
         levelManager.LoadNextLevel ();
     }
 }

Note that I changed the ball’s velocity before destroying the object, this is very important. If I had destroyed it first, then the velocity would never have been changed.