Search Unity

Checkpoint and Respawn

Discussion in '2D' started by Dumitru, Jul 29, 2015.

  1. Dumitru

    Dumitru

    Joined:
    May 7, 2015
    Posts:
    80
    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

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class LevelManager : MonoBehaviour {
    5.  
    6.     public GameObject currentCheckpoint;
    7.  
    8.     private PlayerController player;
    9.  
    10.     // Use this for initialization
    11.     void Start ()
    12.     {
    13.         player = FindObjectOfType<PlayerController> ();
    14.     }
    15.  
    16.     // Update is called once per frame
    17.     void Update ()
    18.     {
    19.  
    20.     }
    21.  
    22.     public void RespawnPlayer()
    23.     {
    24.         Debug.Log("Player Respawn");
    25.         player.transform.position = currentCheckpoint.transform.position;
    26.     }
    27. }
    28.  
    KillPlayer Script

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class KillPlayer : MonoBehaviour {
    5.  
    6.     public LevelManager levelManager;
    7.  
    8.     // Use this for initialization
    9.     void Start ()
    10.     {
    11.         levelManager = FindObjectOfType<LevelManager> ();
    12.     }
    13.  
    14.     // Update is called once per frame
    15.     void Update ()
    16.     {
    17.  
    18.     }
    19.  
    20.     void OnTriggerEnter2D(Collider2D other)
    21.     {
    22.     if (other.name == "Player")
    23.         {
    24.             levelManager.RespawnPlayer();
    25.         }
    26.     }
    27.  
    28.  
    29. }
    Checkpoint Script

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Checkpoint : MonoBehaviour {
    5.  
    6.     // Use this for initialization
    7.     void Start () {
    8.  
    9.     }
    10.  
    11.     // Update is called once per frame
    12.     void Update () {
    13.  
    14.     }
    15.  
    16.     void OnTriggerEnter2D(Collider2D other)
    17.     {
    18.         if (other.name == "Player")
    19.         {
    20.             levelManager.currentCheckpoint = gameObject;
    21.         }
    22.     }
    23.  
    24. }


    Also, I have an error in the CHECKPOINT SCRIPT saying : error CS0103: The name `levelManager' does not exist in the current context
     
    Last edited: Jul 29, 2015
  2. hodispk

    hodispk

    Joined:
    Jun 16, 2015
    Posts:
    19
    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"?
     
  3. Dumitru

    Dumitru

    Joined:
    May 7, 2015
    Posts:
    80
    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" ?
     
  4. Zoltern

    Zoltern

    Joined:
    Jun 16, 2015
    Posts:
    14
    Checkpoint class doesn't have a reference to levelManager:

    1. using UnityEngine;
    2. using System.Collections;

    3. public class Checkpoint : MonoBehaviour {

    4. // Use this for initialization
    5. void Start () {

    6. }

    7. // Update is called once per frame
    8. void Update () {

    9. }

    10. void OnTriggerEnter2D(Collider2D other)
    11. {
    12. if (other.name == "Player")
    13. {
    14. levelManager.currentCheckpoint = gameObject;
    15. }
    16. }

    17. }
    In line 20 you call levelManager.currentCheckpoint but it is not defined for that class.
    public LevelManager levelManager;
    levelManager = FindObjectOfType<LevelManager>();

    Add it and it should work as expected.
     
  5. Dumitru

    Dumitru

    Joined:
    May 7, 2015
    Posts:
    80

    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

    P-S , thanks for helping me out!
     
    Last edited: Jul 31, 2015
  6. Zoltern

    Zoltern

    Joined:
    Jun 16, 2015
    Posts:
    14
    Any error on the console? Is the player passing through the respawner before the spikes? It seems like it should work, what error are you seeing?

    Also, set a debug message when the checkpoint is being set (in the Checkpoint Script).
     
  7. Dumitru

    Dumitru

    Joined:
    May 7, 2015
    Posts:
    80
    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 >.>
     
  8. tedthebug

    tedthebug

    Joined:
    May 6, 2015
    Posts:
    2,570
    Does either the player or the objects have a rigidbody2d?

    Also, have you done the unity tutorials on the unity/learn section? They are a better intro to unity before YouTube.
     
    vakabaka likes this.
  9. vakabaka

    vakabaka

    Joined:
    Jul 21, 2014
    Posts:
    1,153
    or check, that player's name is Player.
     
    tedthebug likes this.
  10. Zoltern

    Zoltern

    Joined:
    Jun 16, 2015
    Posts:
    14
    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:

    1. if (other.name == "Player")
    2. {
    3. Debug.Log("Setting the checkpoint");
    4. levelManager.currentCheckpoint = gameObject;
    5. }
    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.

    Good luck :)
     
    theANMATOR2b likes this.
  11. unhuge

    unhuge

    Joined:
    Jan 3, 2018
    Posts:
    2
    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