- Home /
Re-setting scenario after respawn
Hello guys, I am making a 2D game, where the main objective is to avoid certain traps that the “environment” has for you. Similar to all the ‘I Wanna be the ...’ series. All the traps consists on different events (OnTriggerEnter2d or OnCollisionEnter2D) that my character activates as he walks in the scenario.
The issue is that in many of them, its position is changed. For example in this code:
void OnTriggerEnter2D(Collider2D col){
string name = col.name;
if ( name == "Trap1Detector") {
GameObject trap = GameObject.Find("Trap1") as GameObject;
float xAct = trap.transform.position.x;
float yAct = trap.transform.position.y;
trap.transform.position = new Vector2(xAct,yAct +3);
col.isTrigger = false;
}
}
And if my player trigger one of those events, and then dies, when he respawn the scenario remains changed, with all the traps already "activated".
Is there a way of "re-setting" or returning the scenario to its dfault state (the one that has one the game starts) when my player dies?
I don't want to do move every single trap to its default state by hand...
The respawning script contains the following code:
using UnityEngine;
using System.Collections;
public class Respawn : MonoBehaviour {
public GameObject personaje;
public Transform ptoDeSpawn;
void OnTriggerEnter2D(Collider2D coll){
print ("holi");
Destroy (coll.gameObject);
if (personaje) {
GameObject p = Instantiate (personaje, ptoDeSpawn.position, Quaternion.identity) as GameObject;
CamaraController cam = Camera.main.gameObject.GetComponent<CamaraController> ();
cam.objetivo = p.transform;
}
}
}
THANKS!
Answer by joshpsawyer · Apr 13, 2014 at 10:10 PM
Initially, I was going to suggest just reloading the level, but that doesn't deal with your spawn point. As far as I know, there's no built in way to reset a GameObject, you'll have to do it manually.
On each traps Start() function, you could store it's original state (e.g. transform.position) and then you could implement a Reset() function that simply restores those saved values. You could then call Reset() from your Respawn script?
Alternatively, you could store the ptoDeSpawn (and a flag isRespawning or something) in a PlayerPrefs and reload the entire level, using the stored data as your character's initial spawn point.
Thanks, after some research I could implement what I wanted easily using PlayerPrefs. What I do is saving as floats the transform.position of 'ptoDeSpawn', and then when I do Application.LoadLevel(Application.loadedLevel), the spawn point is set to my new spot :)
Answer by Simon-Larsen · Apr 13, 2014 at 10:14 PM
If you want to reset everything to the way your scene was originally setup, then you can just reload the scene
Application.LoadLevel(Application.loadedLevel);
Your answer
![](https://koobas.hobune.stream/wayback/20220613142637im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Respawning player not working 2 Answers
Changing Scenes on 2D Collision 4 Answers
How to create a save & load system with: System.Serializable ??? 1 Answer