timesHit++; increment not starting

Hey, I am stuck on lecture 077. The OnCollisionEnter2D function is being called, but timesHit++; increment is not starting.

Here is my code -

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class brick : MonoBehaviour {
public int maxHits;
public int timesHit;

void Start () {
timesHit = 0;

void Update () {

void OnCollisionEnter2D(Collision2D col ) {
Debug.Log(“OnCollision function called”);

How do you know it’s not incrementing? Are you sure you’re checking the value for the right brick?

(The best way I can think of checking is to pause the game during play before a brick is about to be hit, go to the scene view and click on that particular brick, and check its value in the inspector. Then do the same thing after the brick is hit. Of course, it will have be a brick that can be hit multiple times.)

If you only have one brick in the scene, then you can also place

Debug.Log ("Times hit: " + timesHit);

in both the Start method and atfter your other Debug.Log statement in the OnCollisionEnter2D method.
Then you should actually see the hit count in the console before and after the hit event occurs.

Privacy & Terms