F
F
FreedomInside2020-03-26 14:43:40
2D
FreedomInside, 2020-03-26 14:43:40

How to reset checkpoints in Unity?

Good afternoon!
There are 3 scripts for working with checkpoints. On Death, the Canvas panel is loaded and there are a couple of buttons. Restarting the scene loads the last checkpoint, but how do you make the Canvas button load the level from the beginning?

spoiler
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class GameMaster : MonoBehaviour
{
    private static GameMaster instance;
    public Vector2 lastCheckPointPos;

    void Awake()
    {
        if(instance == null)
        {
            instance = this;
            DontDestroyOnLoad(instance);
        }
        else
        {
            Destroy(gameObject);
        }
    }
}

spoiler
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Checkpoint : MonoBehaviour
{
    private GameMaster gm;

    void Start()
    {
        gm = GameObject.FindGameObjectWithTag("GM").GetComponent<GameMaster>();
    }

    void OnTriggerEnter2D(Collider2D other)
    {
        if (other.CompareTag("Player"))
        {
            gm.lastCheckPointPos = transform.position;
        }
    }
}

spoiler
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;

public class PlayerPos : MonoBehaviour
{
    private GameMaster gm;

    void Start()
    {
        gm = GameObject.FindGameObjectWithTag("GM").GetComponent<GameMaster>();
        transform.position = gm.lastCheckPointPos;
    }

    void Update()
    {
        if (Input.GetKeyDown(KeyCode.Space))
        {
            SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
        }
    }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
DanielMcRon, 2020-03-26
@DanielMcRon

Eh ... I'm too lazy to even go to Google, so I'll write it myself. Why can't you just google? You make a method with loading a new scene, hang it on the button event handler, when a click on this button works, magic happens - go ahead, fighter

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question