Help with checkpoint system (C#)
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.
Answer by Morgenstern_1 · Oct 14, 2016 at 11:32 AM
DontDestroyOnLoad should be set on the gameObject, not the script I think. Try changing that.
But then you'll have a new problem - you can't instantiate the player in Awake because Awake only gets run once. When you reload the scene it will not get called again, so your character won't spawn.
Actually my problem isn't that it's not spawning the player (it only does that if I move instantiating them to start) the problem is it isn't spawning them in the right spot.
I understand that, read the first half of my answer. To clarify: DontDestroyOnLoad is not getting set properly, therefore your object might be getting destroyed on load. The new object then spawns the player back at the start. You'll hit my second point once you've fixed that.
Okay, I see what you're getting at.
I moved the awake parts to start and fixed DontDestroyOnLoad but it's still not spawning the player at the checkpoint.
Answer by cartersupergames · Sep 12, 2021 at 08:26 PM
Guys Guys Guys, youre doing it all wrong, Just use the function Teleport, like this: Teleport(Gameobject Nameofplayer) 65,574433 75,68765 824,754 Its simple.
Your answer
![](https://koobas.hobune.stream/wayback/20220612094549im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Help with Checkpoint (C#) 1 Answer
Making a counter that does not reflect on time 2 Answers
OnTriggerEnter has a weird bug, help me pls 1 Answer
Stopping an animation when key is unpressed 0 Answers
Check rotation of an obstacle 0 Answers