Hello, I have been following tutorials on youtube on how to do a checkpoint and respawn, the problem is, it doesn’t work, I have tried over 5 times, I wonder if you guys have any ideas on how to make a checkpoint and a respawn, it would be greatly appreciated, I just need a single checkpoint, and when the character hits a spike, he respawns at that checkpoint, any tips or ideas will be greatly appreciated, thank you for taking your time to read this post.
Here are the scripts
Level Manager Script
using UnityEngine;
using System.Collections;
public class LevelManager : MonoBehaviour {
public GameObject currentCheckpoint;
private PlayerController player;
// Use this for initialization
void Start ()
{
player = FindObjectOfType<PlayerController> ();
}
// Update is called once per frame
void Update ()
{
}
public void RespawnPlayer()
{
Debug.Log("Player Respawn");
player.transform.position = currentCheckpoint.transform.position;
}
}
KillPlayer Script
using UnityEngine;
using System.Collections;
public class KillPlayer : MonoBehaviour {
public LevelManager levelManager;
// Use this for initialization
void Start ()
{
levelManager = FindObjectOfType<LevelManager> ();
}
// Update is called once per frame
void Update ()
{
}
void OnTriggerEnter2D(Collider2D other)
{
if (other.name == "Player")
{
levelManager.RespawnPlayer();
}
}
}
Checkpoint Script
using UnityEngine;
using System.Collections;
public class Checkpoint : MonoBehaviour {
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
void OnTriggerEnter2D(Collider2D other)
{
if (other.name == "Player")
{
levelManager.currentCheckpoint = gameObject;
}
}
}
Also, I have an error in the CHECKPOINT SCRIPT saying : error CS0103: The name `levelManager’ does not exist in the current context
Hi @Dumitru :
First lets do the “levelManager” error: is because ‘Checkpoint Script’ doesn’t have a “levelManager” object
Second, do your log print “Player Respawn”?
I’m still new to Unity, Where do I exactly add the “levelManager” in the checkpoint script, also, what do you mean by do youir log print “Player Respawn” ?
In line 20 you call levelManager.currentCheckpoint but it is not defined for that class.
public LevelManager levelManager;
levelManager = FindObjectOfType();
I have added it and the character just walks through the spike
I have added the KillPlayer script to the spikes sprite and have set its box collider 2d to trigger, I have added a gameobject called Checkpoint, and added the Checkpoint script to the Checkpoint. Also made another gameobject and called it LevelManager, and added the LevelManager script to it, the player just walks past the spikes , what am I doing wrong? :s
No error in the console, I have put the player prefab right on top of the checkpoint, and how do I add a debug message in the checkpoint script? I’m confused on why it isn’t working… I’m just trying to make the character to respawn after he gets hit by a spike >.>
Well, ok, so, a tip for programming in general: do one part at a time, always. Break the problem is smaller ones and go step by step.
First thing: move the player over the respawn and add a debug on the colission method to know it is working:
Untill you see that message in the console log, everything else should be secondary. Like the others said, check if the player has the name (why use name and not tag?) Player.
i already success to respawn the player… but i need more than 1 checkpoint… after on collider trigger… it still go to my main checkpoint… what a problem