Okay so. I know exactly what I’m trying to do here, and how I’m trying to do it. Pretty much what I have is a checkpoint manager (with DontDestroyOnLoad) and checkpoint objects. When the player hits the checkpoint, it sets the spawn position in the manager to the checkpoint’s position. So when the player dies, it’s supposed to reload the scene and spawn the player at the new spawn position.
Except, what’s actually going on is when the player dies after hitting the checkpoint, the manager just spawns them at the beginning of the level (set in the inspector rather than in the script).
Here’s the checkpoint manager script:
using UnityEngine;
using System.Collections;
public class CheckpointManager : MonoBehaviour {
[SerializeField] Object Player;
public Vector3 pos;
public Quaternion rot;
// Use this for initialization
public void Awake()
{
DontDestroyOnLoad(this);
if (FindObjectsOfType(GetType()).Length > 1)
Destroy(gameObject);
Instantiate(Player, pos, rot);
}
// Update is called once per frame
void Update () {
Debug.Log ("position = (" + pos.x.ToString ("F0") + "," + pos.y.ToString ("F0") + "," + pos.z.ToString ("F0") + ")");
}
public void Respawn()
{
Application.LoadLevel(Application.loadedLevelName);
}
}
And the checkpoint object script:
using UnityEngine;
using System.Collections;
public class CheckpointObject : MonoBehaviour {
Animator anim;
AudioSource source;
public AudioClip checkSound;
public GameObject PointLaser;
//public int CheckNumber;
CheckpointManager Manager;
public bool IsReached;
[SerializeField] bool IsStartPoint;
// Use this for initialization
void Awake () {
anim = GetComponent<Animator> ();
source = GetComponent<AudioSource> ();
Manager = GameObject.FindGameObjectWithTag("Manager").GetComponent<CheckpointManager>();
}
void Start()
{
/*
if (IsStartPoint) {
Manager.pos = transform.position;
Manager.rot = transform.rotation;
}
*/
}
// Update is called once per frame
void Update () {
anim.SetBool("Reached", IsReached);
if (IsReached)
{
PointLaser.SetActive(false);
}
}
void OnTriggerEnter (Collider col)
{
if (!IsStartPoint) {
if (!IsReached) {
if (col.gameObject.tag == "Player") {
Manager.pos = transform.position;
Manager.rot = transform.rotation;
source.PlayOneShot (checkSound);
IsReached = true;
}
}
}
}
}
I hope someone can help me out here. I don’t know why the manager isn’t spawning the player at the checkpoint.