Variable is not updated

Hello everyone, I’m new to unity and I’ve faced this problem I hope you help me with it.

I’m making a block breaker game like arkanoid game and I’m trying to load the next level when I hit the destroy all the bricks in the game, so I made a static integer variable called (blocksCount) that counts the number of the blocks in the game and each time I destroy a block I decrements the (blockCount) by 1 until it becomes 0 so I load the Win screen.

this is my code :-

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

        public class HitCounter : MonoBehaviour {
	
	private int hitTimes;
	private LevelManager levelmanager;
	public Sprite[] sprites;
	private static int bricksCount=0;
	// Use this for initialization
	void Start () {
		hitTimes = 0;
		levelmanager = GameObject.FindObjectOfType<LevelManager> ();
		if(this.tag=="Breakable")
		bricksCount++;
	}
	
	// Update is called once per frame
	void Update () {
		if (bricksCount <= 0) {
			levelmanager.loadNextLevel ();
		} else
			print ("brlocks remaining are " + bricksCount);


	}
	void OnCollisionEnter2D (Collision2D collision)
	{
		if (this.tag == "Breakable")
			executeAction ();
}

	void executeAction()
	{
		int maxHits = sprites.Length+1;
		hitTimes++; //increasing the total number of block hits
		//TODO ask why we destroy a game object and how does we know that the game object is the specified block
		if (hitTimes >= maxHits) {
			bricksCount--;
			DestroyObject (gameObject);

		}
		else {
			if(sprites [hitTimes - 1]!=null)
			this.GetComponent<SpriteRenderer> ().sprite = sprites [hitTimes - 1];
		}
	}
	}

The problem is that when I destroy the last block in the game it doesn’t load the next level I tried print a message shows how many bricks are still inside the update method, but unfortunatly it says there stills another 1 block but actually all blocks are destroyed.

I’ve fixed that by moving the code from the update method into the ((OnCollisionEnter2D)) method and that worked perffectly.

as follows:-

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

public class HitCounter : MonoBehaviour {
	
	private int hitTimes;
	private LevelManager levelmanager;
	public Sprite[] sprites;
	private static int bricksCount=0;
	// Use this for initialization
	void Start () {
		hitTimes = 0;
		levelmanager = GameObject.FindObjectOfType<LevelManager> ();
		if(this.tag=="Breakable")
		bricksCount++;
	}
	
	// Update is called once per frame
	void Update () {
		
	}
	void OnCollisionEnter2D (Collision2D collision)
	{
		if (this.tag == "Breakable")
			executeAction ();
}

	void executeAction()
	{
		int maxHits = sprites.Length+1;
		hitTimes++; //increasing the total number of block hits
		//TODO ask why we destroy a game object and how does we know that the game object is the specified block
		if (hitTimes >= maxHits) {
			bricksCount--;
			DestroyObject (gameObject);
			if (bricksCount <= 0) {
				levelmanager.loadNextLevel ();
			} else
				print ("brlocks remaining are " + bricksCount);


		}
		else {
			if(sprites [hitTimes - 1]!=null)
			this.GetComponent<SpriteRenderer> ().sprite = sprites [hitTimes - 1];
		}
	}
	}

Could someone tell me why that happened ?? where the previous code is right and thanks in advance.

Hello ,
I think that removing this line of code might solve it.

bricksCount++;

in your start function you are adding one extra brick to your total brick count

void Start () {
         hitTimes = 0;
         levelmanager = GameObject.FindObjectOfType<LevelManager> ();
         if(this.tag=="Breakable")
         bricksCount++;
     }

Please let me know if it solved the problem

I think the problem here is simply of order of execution. You’re updating bricksCount inside the collision code, and in there you destroy the object right away. So what probably was going on is that when the last Update for this object was run, you still had 1 block, and when you would have 0 blocks, the object doesn’t exist anymore.

Your solution is exactly what I’d suggest to do(change level right when the counter is decremented), but you could also set this logic in another object that checks for HitCounter.bricksCount, if it were a public variable instead(or there was some way to access it from outside).