Second play through of game behaves differently than first

LevelManager.cs is working if I start my first play through. However, if I lose and click start from the main menu and beat Level_1, Level_2 does not load.

public void LoadLevel(string name){
	Debug.Log("Level load requested for: " + name);
	Application.LoadLevel (name);
}
public void QuitRequest(){
	Debug.Log ("I want to quit!");
	Application.Quit ();
}
public void LoadNextLevel(){
	Application.LoadLevel (Application.loadedLevel + 1);
}
public void BrickDestroyed(){
	if(Brick.breakableCount <= 0){
		LoadNextLevel();
	}
}

Here’s my Brick.cs in case the issue has anything to do with SimulateWin();

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

private int timesHit;
private LevelManager levelManager;
private bool isBreakable;
// Use this for initialization
void Start () {
	isBreakable = (this.tag == "Breakable");
	//Keep track of breakable bricks
	if (isBreakable){
		breakableCount++;
	}
	timesHit = 0;
	levelManager = GameObject.FindObjectOfType<LevelManager>(); 
}

// Update is called once per frame
void Update () {
}
void OnCollisionEnter2D (Collision2D col) {
	AudioSource.PlayClipAtPoint(crack,transform.position);
	if (isBreakable){
		HandleHits();
	}
}

void HandleHits(){
	timesHit++;
	int maxHits = hitSprites.Length + 1;
	if (timesHit >= maxHits){
		breakableCount--;
		levelManager.BrickDestroyed();
		Destroy (gameObject);}
	else {LoadSprites();
	}
}
void LoadSprites(){
	int spriteIndex = timesHit - 1;
	if (hitSprites[spriteIndex]) {
	this.GetComponent<SpriteRenderer>().sprite = hitSprites[spriteIndex];
	}
}
//TODO Remove this method once we can win
void SimulateWin(){
	levelManager.LoadNextLevel();
}

Which version of Unity are you using? In 2018 (and possibly some earlier versions, though I don’t know which ones off the top of my head) you need to use the Scene Manager as Application.LoadLevel has been deprecated. For the most part, it’s just a syntax change, it behaves in pretty much the same way:

First of all, you’ll need to add

using UnityEngine.SceneManagement;

to the very top of your code.

Inside your LoadLevel() function, instead of Application.LoadLevel you’ll use

SceneManager.LoadScene(name);

And inside the LoadNextLevel() function you’ll use

SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 1);

Hope that helps!

Privacy & Terms