Next Level Activates after first block is destroyed - Counting issue

I need some assistance with the BlockBreaker game. The next level activates when I have destroyed only one brick. I think it is in the counting. It seems that the (breakCount --) doesn’t seem to work. I’m attaching my scripts for Blocks and Level Manager.

Thanks

Bill

Blocks Script

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

public class Blocks : MonoBehaviour {

	public AudioClip crack;
	public Sprite[] hitSprites;
	public static int breakableCount = 0;

	private int TimesHit;
	private LevelManager sceneManager; 

	private bool isBreakable;

	// Use this for initialization
	void Start () {
		isBreakable = (this.tag == "Breakable");

		//Here we keep track of the number of bricks in the game
		if (isBreakable) {
			breakableCount ++;
				print (breakableCount);
		}
			TimesHit = 0;
			sceneManager = GameObject.FindObjectOfType<LevelManager> ();
		}
							 											 	
	void OnCollisionExit2D (Collision2D Collision) {
		AudioSource.PlayClipAtPoint (crack, transform.position);
		if (isBreakable) { 
			HandleHits ();
		}
	}
	void HandleHits  (){
			TimesHit++;
		int maxHits = hitSprites.Length + 1;
		if (TimesHit >= maxHits){
			breakableCount --; 
				print (breakableCount);
				sceneManager.BrickDestroyed(); 
			Destroy(gameObject);		
			} else { 
				LoadSprites ();
			}
		} 
						 			 
	void LoadSprites () {    
		int spriteIndex = TimesHit - 1;
			if (hitSprites [spriteIndex]!= null) {
			this.GetComponent<SpriteRenderer> ().sprite = hitSprites [spriteIndex];
		}
	}
	void SimulateWin (){
		SceneManager.LoadScene(SceneManager.GetActiveScene ().buildIndex + 1);
	}

}

LevelManager

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

public class LevelManager : MonoBehaviour {
	public static int breakableCount;

	public void LoadScene (string name) { 
		breakableCount = 0; 
		SceneManager.LoadScene(name); 
			
	}					
	
	// Update is called once per frame
	public void QuitRequest () {
		Application.Quit ();
	}
						
	public void LoadNextScene (){
		breakableCount = 0; 
		SceneManager.LoadScene(SceneManager.GetActiveScene ().buildIndex + 1);
	}
				
	public void BrickDestroyed () {
		if (breakableCount <= 0) { 
			LoadNextScene ();
		} 
	}						
}

Hi,

Only had a quick look via my mobile, but you are declaring breakableCount in both scripts.

As soon as BrickDestroyed is called for the first time it is checking the variable in LevelManager, not Blocks, thus value is always zero, and as such it loads your next level.

Hope this helps. :slight_smile:


See also;

Rob,

Thanks for your reply. Two questions. I’m new to C#. Can you help me define “declaring”? and Should I take breakableCount out of the Blocks script?

Thanks

Bill

Hi Bill,

My apologies, declaring a variable is where you state a data type and give the variable a name and often state the scope of the variable.

So, for example;

public String message;

In the above line I declare a variable with a public/scope, thus anything will be able to access it.

I state its data type of String, this variable will be used to store some characters.

Finally I give the variable a name, in this case I have named it message.

In your project I believe the breakable count variable is added to the Brick class, Blocks in your case.

This would make sense because it is going to store the total number of breakable bricks/blocks.

It has been defined as static which means that the variable belongs to the class, not a specific instance of that class. As such, you could have 200 blocks in your scene and when they refer to breakableCount the value is retrieved and set at the class level, not the instance. Thus you can destroy a block but the value of this variable isn’t reset.

I believe the variable to remove is this one from your LevelManager.

Then, where you were checking in your BlockDestroyed' method, useBlocks.breakableCount’ instead of just breakableCount.

I hope this helps. :slight_smile:

Rob,

Thanks for your help and your explanations. I’ll make the adjustments and let you know if I can make it work.

All the best,

Bill

Rob,

I’ve made the adjustments as you suggested. My results are still negative. The levels don’t change and my Debug.Log (breakableCount); are not printing to the console. Any suggestions?

All the best,

Bill

Hi Bill,

Probably best if I can have a look at the project as a whole.

Could you zip up your project files and, if its less than 10mb, upload it here, or, if its larger share it via DropBox or Google Drive (or similar).

I will be free in about an hour and a half so can take a look then for you.

Rob,

I’ve loaded my project file in Dropbox and have sent you access via email.

Thanks for all of your help.

Bill

1 Like

No problem, will be able to check in around 40 minutes or so.

Just to check, you’ve zipped the whole project yes?

Rob,

yes.

Bill

Great Bill, thanks.

Not seen any emails yet though, nor any notifications via Dropbox on my mobile.

Which email address did you use for me?

Rob,

Here is the link.

https://www.dropbox.com/sh/3rtcbn3m2pnhmhf/AADCC6FN8aXH33tO-ci1Q-SAa?dl=0 https://www.dropbox.com/sh/3rtcbn3m2pnhmhf/AADCC6FN8aXH33tO-ci1Q-SAa?dl=0

thanks
Bill

PS.

I changed the name of the Block script to “Bricks” and changed all references from “block" to “brick”. The first level worked like a charm. The second level was a no go. I also found the Lose Collider in the prefab file. I changed the Bottom Collider out for the LoseCollider and kept the Lose script for the LoseCollider.

Thanks

Bill

Taking a look now…


Updated Mon Oct 23 2017 22:04

Ok Bill (@William_Mayfield), so I ran the game and there are a couple of things I’ve spotted.

  • When the ball hits some of the blocks it seems to be able to simply move through/over them
  • In the console, the breakableCount variable totals 25, yet you only have 22 blocks which are visible.

On further inspection it would seem that you have applied the Blocks script component to three other items in the Hierarchy as shown below;

image

image

These also had the Breakable tag set, so, you effectively have 3 extra blocks, but these not only cannot be seen, they also have no colliders, so they will always exist. Because of this, the breakableCount variable is never going to hit zero and you won’t be taken to the next level.

To resolve this, remove the Block script component from the following GameObjects in the Hierarchy;

  • Blocks
  • Level One
  • Level Two

By making these changes the auto-played game will now successfully load the next scene.

The other issue, regarding the blocks not getting “hit”, that is down to this;

image

This is one of your blocks, the green square in the middle is the BoxCollider2D component, as you can see, it isn’t the same size as the block, as such, if the ball goes either side of it, the ball will simply pass through the block.

The easiest way to fix this is;

  • Drag the yellow block prefab into the Hierarchy
  • Double-click on it in the Hierarchy to focus the view in the scene view
  • Within the Inspector, locate the Box Collider 2D component and remove it (click on the cog on the far right and a menu appears)
  • Click on the Add Component button and re-added a BoxCollider2D
  • Click on the Apply button at the top of the Inspector
  • Within the Hierarchy, right-click and select Delete for the yellow block prefab instance

Repeat the above steps for the red block prefab and the green block prefab.

All of your blocks in your scene(s) will now be corrected and you will not need to individual size the BoxCollider2D components on the blocks.

Hope this helps :slight_smile:

Rob,

I fixed it!!! Woo Hoo!!!

I went into the inspector under each of the bricks and renumbered them for all three levels. I also changed the the empty game objects that I was storing the bricks and changed them to untagged and eliminated the scripts that had become attached in all of the thrashing about I had been doing for a week. I did this as a result of changing the name of my block script to Brick and followed the lecture on name changes.

I’ve tested the game for two full run throughs and it works just fine!!! I will test is for a few more times.

Thank you for your time and commitment to my success.

I’m open to suggestions to what would make programing easier.

All the best,

Bill

1 Like

Hi Bill,

Great to hear you can move forward again with this, and you are very welcome.

Privacy & Terms