Search Unity

Checkpoints not working

Discussion in 'Scripting' started by HarryWhitelegg02, Mar 20, 2020.

  1. HarryWhitelegg02

    HarryWhitelegg02

    Joined:
    Mar 11, 2020
    Posts:
    39
    For some reason my checkpoints are not working in my 2D platformer game, the checkpoint is meant to be activated when the player collides with it but for some reason, the checkpoint doesn't activate and the player just respawns at the first checkpoint.
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Checkpoint : MonoBehaviour
    6. {
    7.     public GameMaster gameMaster;
    8.  
    9.     // Start is called before the first frame update
    10.     void Start()
    11.     {
    12.         gameMaster = FindObjectOfType<GameMaster>();
    13.     }
    14.  
    15.     void OnTriggerEnter2D(Collider other)
    16.     {
    17.         if (other.gameObject.tag == "Player")
    18.         {
    19.             gameMaster.spawnPoint = gameObject.transform;
    20.  
    21.         }
    22.     }
    23. }
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class GameMaster : MonoBehaviour
    6. {
    7.     public static GameMaster gm;
    8.     public Transform playerPrefab;
    9.     public Transform spawnPoint;
    10.  
    11.  
    12.  
    13.     // Start is called before the first frame update
    14.     void Start()
    15.     {
    16.         if (gm == null)
    17.         {
    18.             gm = GameObject.FindGameObjectWithTag("GM").GetComponent<GameMaster>();
    19.         }
    20.  
    21.     }
    22.  
    23.     public void RespawnPlayer()
    24.     {
    25.         Debug.Log("Player Respawn");
    26.         playerPrefab.transform.position = spawnPoint.transform.position;
    27.     }
    28.  
    29. }
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Dead : MonoBehaviour
    6. {
    7.     public GameMaster gameMaster;
    8.  
    9.  
    10.     void Start()
    11.     {
    12.         gameMaster = FindObjectOfType<GameMaster>();
    13.     }
    14.  
    15.     void OnTriggerEnter2D(Collider2D other)
    16.     {
    17.         if (other.gameObject.tag == "Player")
    18.         {
    19.            
    20.                 Debug.Log("Died");
    21.  
    22.                 gameMaster.RespawnPlayer();
    23.  
    24.                 other.gameObject.GetComponent<PlayerMovement>().Die();
    25.  
    26.         }
    27.  
    28.     }
    29. }
    I am a beginner at unity and c# so if someone could please help it would be much appreciated.
     
  2. Nyxal_Indie

    Nyxal_Indie

    Joined:
    Jun 26, 2019
    Posts:
    179
    Inside Checkpoint script you have
    Code (CSharp):
    1. void OnTriggerEnter2D(Collider other)
    2.     {
    3.         if (other.gameObject.tag == "Player")
    4.         {
    5.             gameMaster.spawnPoint = gameObject.transform;
    6.         }
    7.     }
    Fix it writing void OnTriggerEnter2D(Collider2D other)
     
    HarryWhitelegg02 and Kurt-Dekker like this.
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,697
    Oooh good eye!!

    Also, to OP, when you have weird "just doesn't work" type problems, always put lots of Debug.Log() statements in all over so you know what is executing and what isn't. In this case the incorrect signature that Nyfirex noted above would definitely cause that method to never get called, and that would draw your attention immediately to that function.
     
    HarryWhitelegg02 likes this.
  4. HarryWhitelegg02

    HarryWhitelegg02

    Joined:
    Mar 11, 2020
    Posts:
    39
    Cheers mate, good eye for spotting that.