So I made a respawn function and what is supposed to happen is the script it supposed to load a saved checkpoint. However I need to reset the scene (and again this worked just fine last night which was odd...).
Essentially the checkpoint waits for a player and then when the player enters the current checkpoint is written to a .txt file. Upon reload or death the scene is restarted, the .txt file is read and grabs the checkpoint. Everything works fine without Scene scene = SceneManager.GetActiveScene(); SceneManager.LoadScene(scene.name); //Reload scene.
but I need that in order to "refresh" the scene.
CheckPoint Code:
using UnityEngine;
using System.Collections;
public class Checkpoint : MonoBehaviour {
public LevelManager levelManager; //Empty level manager.
// Use this for initialization
void Start()
{
//levelManager = FindObjectOfType<LevelManager>(); //Unnneeded
}
void OnTriggerEnter(Collider coll)
{
if (coll.tag == "Player")
{
//levelManager.currentCheckPoint = gameObject; //<-DISABLE WHEN ES2 is fixed!!!
Debug.Log("Activated Checkpoint " + transform.position);
ES2.Save(gameObject.name, "myFile.txt?tag=checkpoint"); //Saves checkpoint to file.
}
}
}
LevelManger Code:
using UnityEngine;
using System.Collections;
using UnityEngine.SceneManagement;
public class LevelManager : MonoBehaviour {
[Tooltip("Note this will also serve as the spawnpoint for the player!")]
public GameObject currentCheckPoint;
private GameObject player;
private int PHP;
public int RespawnHP; //Amount of health to give player upon respawn.
void Awake ()
{
}
// Use this for initialization
void Start ()
{
player = GameObject.FindGameObjectWithTag("Player");
}
// Update is called once per frame
void Update ()
{
PHP = GameObject.Find("InventoryManager").GetComponent<InventoryManager>().PlayerHealth;
if (PHP <= 0)
{
Debug.Log("Player HP has been depleted. Respawning.");
RespawnPlayer();
}
if (Input.GetKeyDown("r"))
{
Debug.Log("Restarting level!");
RespawnPlayer();
}
}
public void RespawnPlayer()
{
Scene scene = SceneManager.GetActiveScene(); SceneManager.LoadScene(scene.name); //Reload scene.
currentCheckPoint = GameObject.Find(ES2.Load<string>("myFile.txt?tag=checkpoint")); //Load saved checkpoint.
Debug.Log("Player Respawning...");
player.transform.position = currentCheckPoint.transform.position;
GameObject.Find("InventoryManager").GetComponent<InventoryManager>().PlayerHealth = RespawnHP;
}
void RestartLevel()
{
//ToDo: Restart level, and reload last check point.
}
public static void SaveLevel() //Saves the current level to file.
{
//ES2.Save(SceneManager.GetActiveScene().name, "myFile.txt?tag=levelName"); //Saves level to file.
//ES2.Save("none", "myFile.txt?tag=checkpoint"); //Overwrite checkpoint?
}
}
Does anyone have a clue what is wrong? Like I said earlier the problem seems to arise on scene reloading.
Response to comment(s):
What behaviour are you observing at the moment? - Jack
Currently what happens is the player enters a checkpoint, the checkpoint is saved to a file, and upon respawn the checkpoint is loaded. After this I get spawned to the checkpoint that was saved but then immediately the level reloads itself and the player is not respawnd at the checkpoint. Here is a video to better demonstrate the issue (it will take a minute or two to finish up processing here as I've just pasted the link).