- Home /
How can I make it so that the Level Reloads whenever the Player touches certain Colliders or Triggers?
I'm making a Game and I'm struggling with making the Scene Restart when the Player Collides with Certain Objects. I already searched for this Question many Times but I didn't seem to find anything, that works. I hope you guys can help me. EDIT: Please also tell me, if I should attach your Script to the Player or to the Deadly Obstacles? I'm using 2D.
Answer by TanselAltinel · May 07, 2018 at 06:34 PM
If you are trying to load the same scene, just use SceneManager.LoadScene("CurrentSceneName");
But if you are trying to load from a check point, then you should implement a saving mechanism to spawn your player at desired position and other game objects too.
You should avoid reloading a scene anyway. Resetting default states is always much faster than reloading the scene.
And How can I reload Default States without Reloading the Entire Scene?
You save them first :)
You may want to do this per object, or per scene.
Per object can be done with a simple component like this :
public class StartStateHolder : $$anonymous$$onoBehaviour
{
private Vector3 startPosition;
private void Start ()
{
startPosition = transform.position;
}
public void ResetState ()
{
transform.position = startPosition;
}
}
Do you mean like this:
void OnTriggerEnter () //or ►void OnTriggerEnter2D () {◀ when using 2D, like me
{
Scene$$anonymous$$anager.LoadScene ("CurrentSceneName");
}
Also, I need to know: Should I attach this Script to the Player or to the Deadly Obstacles?
Yeah, something like that. You can attach this script to deadly obstacle, but you gotta make sure to have player triggering this areas. You can do that like this:
void OnTriggerEnter2D(Collider2D other) {
if (other.gameObject.CompareTag("Player")) {
Scene$$anonymous$$anager.LoadScene ("CurrentSceneName");
}
}