3
\$\begingroup\$

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).

\$\endgroup\$
  • \$\begingroup\$ What behaviour are you observing at the moment? \$\endgroup\$ – zcabjro Jun 25 '16 at 14:23
  • \$\begingroup\$ @Jack I just finished editing the question to address that question. \$\endgroup\$ – Sie Jun 25 '16 at 16:42
  • \$\begingroup\$ Is the scene loading an asynchronous task? If so it might begin, and then complete after the checkpoint file is loaded. In which case you should load the scene, wait for completion and then load the checkpoint file. \$\endgroup\$ – DavidColson Jun 25 '16 at 17:00
  • \$\begingroup\$ @Constan7ine Nope. The scene is not loading or completing any asynchronous tasks. \$\endgroup\$ – Sie Jun 25 '16 at 17:21
2
\$\begingroup\$

In your RespawnPlayer() method you are loading a scene at first. Then checking current position, then translating the player to the checkpoint.

Upon loading a new scene, everything resets. In RespawnPlayer() after second line execution, the level manager restarts from Start() ignoring the current checkpoint and player translate codes in RespawnPlayer(). In my first solution I just took three line from your RespawnPlayer() and put them Start(). Another line is just precaution.

Set your player object's position at Start().

Changes in your LevelManager script:

void Start ()
{
    player = GameObject.FindGameObjectWithTag("Player");
    //Get current checkpoint from file
    if(emptyFile)
    {
    currentCheckPoint = vector3.zero;//or default position
    }
    player.transform.position = currentCheckpoint.transform.position;
    //Set health value to player
}

public void RespawnPlayer()
{
Scene scene = SceneManager.GetActiveScene();
SceneManager.LoadScene(scene.name);
}

Or Instantiate your player at Start(). It's handy when you control multiple levels from Level Manager. Keep your "player" named prefab at Resources folder.

void Start ()
{
    //Get current checkpoint from file
    if(emptyFile)
    {
    currentCheckPoint = vector3.zero;//or default position
    }
    Instantitate(Resources.Load("player"), currentCheckpoint.transform.position, Quaternion.identity);
    //Set health value to player
}
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.