Restarting a Level now that Application.LoadLevel is Obsolete

Since the only answer for my question is from Unity 4 it’s not sufficient. What I need is a Current line for the code i have to make my game Restart when I press the R button. There is another problem my " Press R to restart " text is not showing up in the game after i blow myself up. Here’s the Code, i have several Other Scripts too that this one Calls out to.

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

public class GameController : MonoBehaviour {

	public GameObject hazard;
	public Vector3 spawnValues;
	public int hazardCount;
	public float spawnWait;
	public float startWait;
	public float waveWait;

	public GUIText scoreText;
	public GUIText restartText;
	public GUIText gameOverText;

	private bool gameOver;
	private bool restart;
	private int score;

	void Update ()
	{
		if (restart) 
		{
			if (Input.GetKeyDown (KeyCode.R)) 
			{
				SceneManager.LoadScene(SceneManager.GetActiveScene().name);
			}
		}
	}

	void Start()
	{
		gameOver = false;
		restart = false;
		gameOverText.text = "";
		restartText.text = "";
		StartCoroutine (SpawnWaves ());
		score = 0;
		UpdateScore ();
	}

	IEnumerator SpawnWaves()
	{
		yield return new WaitForSeconds (startWait);
		while (true) 
		{
			for (int i = 0; i < hazardCount; i++) {
				Vector3 spawnPosition = new Vector3 (Random.Range (-spawnValues.x, spawnValues.x), spawnValues.y, spawnValues.z);
				Quaternion spawnRotation = Quaternion.identity;
				Instantiate (hazard, spawnPosition, spawnRotation);
				yield return new WaitForSeconds (spawnWait);
			}
			yield return new WaitForSeconds (waveWait);

			if (gameOver == true) 
			{
				restartText.text = "Press 'R' to Restart";
				restart = true;
				break;
			}
		}
	}

	public void AddScore (int newScoreValue)
	{
		score += newScoreValue;
			UpdateScore();
	}

	void UpdateScore ()
	{
		scoreText.text = "Score: " + score;
	}

	public void GameOver ()
	{
		gameOverText.text = "Game Over ";
		gameOver = true;
	}
}

Are you using void OnGUI for your GUIText?

4 Answers

4

This will load the current level you are currently running:

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

In the provided code you never run “GameOver()” function to set the gameover bool to true.

thats exactly what I did, but I used Application.loadlevel instead :)

Again, this does not work for games with lot of assets or the level which was loaded asynchronously.

I guess I might be showing my age a bit.

You must use the new semantic, adding at first :
using UnityEngine.SceneManagement;

Then, when you want restart a level, you can simply do :
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);

So, in order to load the next level, you can do :
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 1);

Simply, Application.LoadLevel(Application.LoadedLevelName);

This will simply reload the level.

@AndrewBilotti Application.LoadLevel is obsolete. Use [SceneManager][1] [LoadScene][2] instead. [1]: http://docs.unity3d.com/ScriptReference/SceneManagement.SceneManager.html [2]: http://docs.unity3d.com/ScriptReference/SceneManagement.SceneManager.LoadScene.html

well, scene manager doesn't work as well, who gives a crap if application.loadlevel is obsolete? (I have like 245 warnings saying that every time I run any of my games) xD

Also, what is with the down votes :(

make sure you’re using UnityEngine.SceneManagement;