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;
}
}
Answer by ByteVault · Feb 16, 2016 at 07:22 PM
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 ins$$anonymous$$d :)
And that's why your code is going to break sooner than @xN0tiCx's... :)
Well, Ive been using it for 5 years and so I remember all of the application.* stuff :P
The code that @Bleackk provided us is using the new "Scene$$anonymous$$anager" and therefore Im providing a snippet of a code which would be equal to what he seeks. What he is using is actually a working piece of code too and Im simply giving him another way of doing it.
I dont want to be rude @AndrewBilotti but the question is not really answered since he also states that the text "Press 'R' to Restart" is never shown. Which means that the "GameOver ()" function is never triggered to set "gameover" bool to "true". Which in return gives "restart" bool a constant state of false ending his ability to press the "R" key in order to change/restart the scene.
(Assu$$anonymous$$g all the other GUIText is working just fine since it is not stated otherwise.)
HOWEVER, I did enjoy the old "Application.loadlevel" very much and right now its not more than a warning and is working just fine. Later though, it will become an error.
Or am I completely off and should perhaps go take a well overdue nap? hehe
Cheers.
Again, this does not work for games with lot of assets or the level which was loaded asynchronously.
Answer by Geckoo · Sep 12, 2016 at 01:16 AM
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);
Answer by AndrewBilotti · Feb 16, 2016 at 02:52 AM
Simply, Application.LoadLevel(Application.LoadedLevelName);
This will simply reload the level.
@AndrewBilotti Application.LoadLevel
is obsolete. Use Scene$$anonymous$$anager LoadScene ins$$anonymous$$d.
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
Ive done Unity for 2 years and literally only discovered that App.LoadLevel is obsolete in the last 2 $$anonymous$$utes. This is a very recent thing.
I think the downvotes came from the "Who gives a crap" part in the comments :D
Its very important to address obsolescence.
xD Idk why it is obsolete but scene manager is buggy :o
I imagine it's for giving bad advice... and you really should give a crap about those 245 warnings - they're there to help you, the developers didn't write them just for fun!
Indeed! Deprecation warnings now are errors later!
Your answer
Follow this Question
Related Questions
How to Restart game (Menu and Game are in same Scene)? 1 Answer
How to add a restart to my 2D game? 0 Answers
how to write script to restart game after game over 1 Answer
Checking variables in GameController from a gameobject? 0 Answers
dragging multiple game objects simultaneously (mobile) 0 Answers