P
P
piffo2020-05-07 01:40:36
C++ / C#
piffo, 2020-05-07 01:40:36

Level menu in the game?

There is a level menu. When you click on the level button, you are transferred to the level itself. The idea is that all level buttons are initially gray and only when the player has already visited the level itself does the button turn white. The name of the button in the editor is the number of the level itself, that is, level 1, the name of the button is 1. There is a PlayerPrefs int, called levelNum, it equals the last level the player visited. Wrote this script

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

public class jump : MonoBehaviour
{
    public Text puk;//текст который должен становится белым
    static bool bol = false;//статическая булевая что бы если levelNum уже не равняется названию кнопки, кнопка оставалась белой

    private void Awake()
    {
        string name = this.gameObject.name;
        int num;
        int.TryParse(name, out num);//конвертируем название кнопки из string в int

        if (num == PlayerPrefs.GetInt("levelNum"))
        {
            bol = true;
        }
        if (bol)
        {
            puk.color = Color.white;
        }
    }
}

Such script weighs on each button. Everything works, but there is one problem. If we got to one level, then all the numbers become white. I realized that this is due to the fact that one script weighs on all buttons. How can this be fixed? Do not write forty scripts for 40 buttons)

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