Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unity3d restart current scene

Tags:

c#

unity3d

Ok so I am trying to restart the scene on R being pressed and for some reason, I am getting errors like, well in the unity console: "unexpected symbol '}' " and "parsing error". But then in Microsoft visual studio I'm getting "; expected". Any ideas of what's wrong with the following code?

void Update() {
    if (Input.GetKeyDown(KeyCode.R))  
        SceneManager.GetActiveScene().buildIndex
}
like image 771
Casper Powell Avatar asked Dec 10 '22 13:12

Casper Powell


2 Answers

You must ask the scene manager to load the scene using LoadScene

if (Input.GetKeyDown(KeyCode.R))  
    SceneManager.LoadScene( SceneManager.GetActiveScene().buildIndex ) ;

You were just retrieving the build index of the current scene.

Also, about yourcompilling error, you have forgotten the semi-colon at the end of the line ;)

like image 197
Hellium Avatar answered Dec 13 '22 03:12

Hellium


I can say that the code below might be the easiest method;

SceneManager.LoadScene(SceneManager.GetActiveScene().name);
like image 23
Mehmet Bütün Avatar answered Dec 13 '22 02:12

Mehmet Bütün