system
1
So, i want to create an Arkanoid clone (just for learning), and i need some help with the loading of the scenes. So, i’ve created a Box collider at the bottom of the screen, to which i’ve attached the next script:
public class LoseCollider : MonoBehaviour {
public LevelManager levelManager;
public void OnTriggerEnter2D (Collider2D trigger) {
levelManager.LoadLevel ("lose_screen");
}
}
This one takes me to a new scene, called lose_screen, in which i have a button for Restart level. My LevelManager (the script that manages the load of my scenes) looks like this:
public class LevelManager : MonoBehaviour {
public void LoadLevel (string name){
SceneManager.LoadScene (name);
}
}
And now i am lost. I think i have to check first what scene was active, then reload that specific scene. But i don’t know how to do it, and where. Can anyone help?
Thanks in advance!
This is untested, off the top of my head:
public void ReloadLevel(){
SceneManager.LoadScene(SceneManager.GetActiveScene);
}
This returns the current scene:
@tomekteyon
Because you never need the name of the scene. What you said is also unnecessary.
system
2
And why you not simply call function LoadLevel from LevelManager with name of scene you started (guess the one with actual game), when user press RestartButton? After load it will be in its initial state.
Hi, I don’t know solved it or not, but may be it will be helpful for other beginners!
For the first you should be know more about general question “How to pass data between scenes – Static variables”
From link you can find simple unity project example how to do it
For solving current challenge you should do next steps
-
Create new script “KeepData” you don’t need assign it to gameobject
public class KeepData : MonoBehaviour {
static public string keepLevelName;
}
-
use this in your class LoseCollider to get level name and store it to static variable
using UnityEngine.SceneManagement;
public class LoseCollider : MonoBehaviour {
void Start()
{
KeepData.keepLevelName = SceneManager.GetActiveScene().name;
}
}
-
in your class LevelManager call the function which will be use stored data from static variable
using UnityEngine.SceneManagement;
public class LevelManager : MonoBehaviour {
public void LevelRestart ()
{
SceneManager.LoadScene(KeepData.keepLevelName, LoadSceneMode.Single);
}
}
don’t forget to call method LevelRestart in you Button component
Segy
3
Try to load scene from the LoseCollider script. See if that works first.
The code below can be said to be the easiest method:
SceneManager.LoadScene(SceneManager.GetActiveScene().name);