[Solved] LoadNextLevel (); Activates after the 1st brick is destroyed

Hello,
As mentioned in the title, the LevelManager.cs seems to take the first brick being destroyed as the last one… So after any brick is destroyed in a level, the next one loads immediately. It’s a nice clear code to have, but i’d like the player to play the game first…

Here is the Code for the Brick.cs

public class Brick : MonoBehaviour {

	public Sprite[] hitSprites;

	public  static int breakableCount;

	private int TimesHit;
	private LevelManager levelManager;

	bool isBreakable;


	// Use this for initialization
	void Start ()
	{
		breakableCount = 0;
		isBreakable = (this.tag == "Breakable");
	//Here we will keep track of the number of bricks in the game
		if (isBreakable) {
		breakableCount ++;
		}
		TimesHit = 0;
		levelManager = GameObject.FindObjectOfType <LevelManager>();
	}
	void OnCollisionEnter2D (Collision2D Collision)
	{
		if (isBreakable) {
			HandelHits ();
		}
	}

	void HandelHits ()
	{
		int MaxHits = hitSprites.Length + 1;
		TimesHit ++;
		if (TimesHit >= MaxHits) {
			breakableCount --;
			levelManager.BricksDestroyed ();
			Destroy (gameObject);
		} else {
		LoadSprites();
		}

	}

	void LoadSprites ()
	{
	int spriteIndex = TimesHit - 1;
		if (hitSprites [spriteIndex]){
	this.GetComponent<SpriteRenderer> ().sprite = hitSprites [spriteIndex];
		}
	}

	//TODO Reomve this method once doen
	void SimulateWin ()
	{
	levelManager.LoadNextLevel ();
	}
	void Update () {

		
	}
}

And here is the Code in LevelManager.cs


public class LevelManager : MonoBehaviour {

public void LoadLevel (string name)
	{
	Debug.Log ("Level Load Requested for " + name);
	Application.LoadLevel (name);
	}

public void QuitGame ()
	{
	Debug.Log ("I'm leaving...");
	Application.Quit ();
	}

	public void LoadNextLevel () {
	Application.LoadLevel (Application.loadedLevel + 1);
	}

	public void BricksDestroyed () {
		if (Brick.breakableCount <= 0) {
		LoadNextLevel ();
		}
	}
}

Thank you

1 Like

Try printing the breakableCount to the console, to see if it gets set correctly. Also check that your brick prefabs are tagged as “Breakable”.

1 Like

Hello @uhadi,

Your problem here is that you are setting breakableCount = 0 in the Start() method.

breakableCount is a static member of the Brick class, e.g. it only exists once, regardless of how many Brick instances you create.

Each time you create a new Brick instance in your scene, you are resetting breakableCount to zero.

Your code to detect whether the last brick has been destroyed is actually working correctly, you are just incorrectly recording the number of breakable bricks.

Remove the following line from your Start() method;

breakableCount = 0;

Hope this helps :slight_smile:

Thank you both

So it was the strat() Method issue. i Really have to digest what a static means, i guess i’m not quite there yet.

Thank you very much :slight_smile:

1 Like

You’re more than welcome.


Normally when you have an instance of a class, if it has its own variables, those not set as static, you can change the values of them and they will only be changed for that specific instance.

Let’s say I have a Person class and that it has a variable for the person’s name;

public class Person
{
    private string _name;

    public string Name
    {
        get { return _name; }
        set { _name = value; }
    }
}

In the above example, I have added a property called Name which can be used to both get and set the value of the instance’s _name variable.

This class could then be used as follows;

public void CreatePeople()
{
    Person personOne = new Person();
    Person personTwo = new Person();
}

In the above method, we create two instances of the Person class, both of which have an instance variable of called _name, neither of which are yet set, let’s sort that out.

public void CreatePeople()
{
    Person personOne = new Person();
    personOne.Name = "Uhadi";

    Person personTwo = new Person();
    personTwo.Name = "Rob";
}

Now, with the above we have two instances of ther Person class, both of which have separate values for their _name variable.

If you declare a variable to be static you are stating that the variable belongs to the class, not a specific instance of that class, as such, there will only be one instance of that variable. Whenever you change its value, you change it across all instances of that class.

If we had set our _name variable as static in our class example above we would need to access it via the class, not one of our two instances, for example;

    string name = Person.Name();    // via the class

as opposed to;

    string name = personOne.Name();    // via the instance

I hope the above is of some use. :slight_smile:

Oh that makes sense, thank you Rob for clarifying :slight_smile:

All the best,

1 Like

No problem at all :slight_smile: