D
D
Dmitry2020-08-15 22:35:01
Unity
Dmitry, 2020-08-15 22:35:01

How to implement level scripts and their connection with checkpoints?

I am learning Unity. I decided to make a small game, divided into several levels. Each level has its own mechanics. For example, on the first level, you need to walk around the house and find a few items, then go to the door (checkpoint) and go to the second level. On the second one, you need to run to the checkpoint for a while. Etc.

The question arose: how to implement scripts for each level (LevelScript_01, LevelScript_02, etc.)?

I tried to make a folder with scripts for each level, each of which has similar functions, such as moving to another level, as well as unique conditions for passing (item counter in 1st, timer in 2nd, whether the boss was defeated in 3rd, and so on). I also made a checkpoint prefab (a trigger that sends a signal to the level script that the player wants to go to another level). The player touches the trigger -> the trigger sends a signal to the LevelScript_01 script -> the level script checks the passing conditions -> the player goes to the second level.

Each scene has a SceneObserver object that contains a single script - the level's numbered script.

But a problem arises: the signal from the checkpoint to a certain level script is not transmitted (or I don’t understand how to do it). It is not logical to make one script in which the conditions for passing all levels at once will be indicated, after which it is not logical to pass the checkpoint argument at what level we are at.
The same problem with items that need to be collected. How to bind them exactly to the first level, and not, for example, to the fifth (where such objects will also be)?

It would be great if you could create a Level field in the checkpoint script, and then, through the inspector, put the script I need in this field, which definitely has a method for moving to the next location. But I'm not sure if this is possible.

How to properly organize the architecture and communication between scene objects without creating dozens of checkpoint prefabs for each level?

Example script for the first level
using System.Collections;
using System.Collections.Generic;
using UnityEngine.SceneManagement;
using UnityEngine;

public class Level_01 : MonoBehaviour
{
    int itemsCollected = 0; // предметов собрано
    int itemsToCollect = 3; // сколько надо собрать
    bool allItemsCollected = false;

    void OnItemCollect()
    {
        itemsCollected += 1;
        Debug.Log("Вы собрали объект");
        if (itemsCollected >= itemsToCollect)
            allItemsCollected = true;
    }

    // этот метод есть в каждом скрипте уровня 
    void OnCheckpointEnter() // метод должен вызываться из префаба чекпоинта (но этого сделать не получается)
    {
        if (allItemsCollected) {
            SceneManager.LoadScene("Level_02");
            Debug.Log("Переход на новый уровень");
        }
    }
}


Sample checkpoint code
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Checkpoint : MonoBehaviour
{
    [SerializeField] private int level_ID; // номер уровня, на котором мы находимся

    Collider col;

    void Start()
    {
        col = GetComponent<Collider>();
    }

    private void OnTriggerEnter(Collider other)
    {
        // отсюда надо вызвать метод конкретного скрипта уровня (для этого я в начало добавил id)
    }
}

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question