How to go beyond Level 3?

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

public class brickScript : MonoBehaviour
{
static int numbricks = 0;
public int hitPoints = 1;

// Use this for initialization
void Start () {
	numbricks++;
}

// Update is called once per frame
void Update () {

}
void OnCollisionEnter ( Collision col ) {
	hitPoints--;

	if (hitPoints <= 0) {
		GameOver ();
	}
}

	void GameOver () {
		Destroy (gameObject);
	numbricks--;
	if (numbricks <= 0) {
		SceneManager.LoadScene ("level 2");
	} else if (numbricks <= 0) {
		SceneManager.LoadScene ("level 3");
	} else if (numbricks <= 0) {
		SceneManager.LoadScene ("level 4");
			
	}

}

}

This is what i have so far, after playing level one it goes to level two but it will not go to level 3 or 4 i have been stuck on this for days, I don’t even have a game over or win scene yet. And yes i have all the levels in build settings and in order from level 1-4. Please help i am new to Unity and Scripting

Hi,

Your problem comes from these lines: if (numbricks <= 0)

In all of your if statements, you put the same condition. Unity will always pick the first one to come up. In your case it will be “load level 2”.

Imagine a similar case. If you say “if red then go right, but if red then go left”, you can imagine that it will always go right when the computer will read the sentence: first instruction is true, does the correlated action and then quit. You have to put it this way “if red then go right but if GREEN then go left”

Back to your case, you have to put a more specific condition in your different if/else if statements. Like:

if (numbricks == 0) {
         SceneManager.LoadScene ("level 2");
     } else if (numbricks == -1) {
         SceneManager.LoadScene ("level 3");
     } else if (numbricks == -2) {
         SceneManager.LoadScene ("level 4");     
}

Edit: I’ve read again your post and since numbrick is initialized at 1 and that the only time you change its value its by substracting 1 … you’ll only and ever get numbricks = 0, thus load Level 2. You have to find an other way of changing the value of numbricks so it can have different values to fill the if statements conditions …