Reload Current Scene with Scene-manager
Hello, I need helps with the scripting for reloading the current scene.
I know there is already similar discussion and answer about this but it doesn't work out mine for some reason. I am not expert coding but this should work?
I am making a 3D floating platform game where if you fell out the platform, you hit R key to restart the current scene but the script doesn't work for some reason;
using UnityEngine;
using System.Collections;
public class reload : MonoBehaviour{
void Update(){
if(Input.GetKeyDown(KeyCode.R)){
Scene scene = SceneManager.GetActiveScene(); SceneManager.LoadScene(scene.name);
}
}
}
Can anyone tell me what wrong with these code or me? thanks
Answer by ghostmode · Oct 18, 2017 at 04:01 AM
I think you're missing a 'using' statement at the top of your script:
using UnityEngine.SceneManagement;
In future, if something's not working then check the console for errors!
Answer by Owen-Rowley · Oct 18, 2017 at 10:20 PM
Sorry I've tried but still no avail; the code is below;
using UnityEngine.SceneManagement;
using System.Collections;
public class reload : MonoBehaviour{
void Update(){
if(Input.GetKeyDown(KeyCode.R)){
Scene scene = SceneManager.GetActiveScene(); SceneManager.LoadScene(scene.name);
}
}
}
The console saying the error is at fourth line, saying "The type or namespace name 'MonoBehaviour' could not be found (are you missing a using directive or an assembly reference)"
thanks
Thanks for posting the error :)
You've now removed the using UnityEngine;
statement from the top of the file, just add it back.
Answer by K0n4N · Jun 02 at 07:50 AM
An old question but a nicer way to do this is:
SceneManager.LoadScene(SceneManager.GetActiveScene().name);
Your answer
Follow this Question
Related Questions
Loading Next Scene 2 Answers
how can i save highest played level?,how can i save number of highest played level? 0 Answers
How can I handle scripts correctly? What's a good way to apply scripts to GameObjects? 1 Answer
[Question] Unity C# Set a Player Class and refence issue 0 Answers
Teleporting problems 1 Answer