G
G
Gregory2562021-10-27 20:36:53
Unity
Gregory256, 2021-10-27 20:36:53

How to create a pause button on each level?

Each scene should have a pause button, manually casting through the inspector is not an option. I tried to write a script, threw it into the main menu on stage 0. The problem is that the pause button crashes only on stage 0. And creates 2 buttons. Could you tell me where to go and how to fix this script.

public GameObject canvasLevel;
    [SerializeField] private int startIndex = 1;
    private Scene[] level;

    private void Start()
    {
        var sceneCount = SceneManager.sceneCountInBuildSettings;
        level = new Scene[sceneCount - startIndex];     
        for (int i = startIndex; i < sceneCount; i++)
        {
            level[i] = SceneManager.GetSceneByBuildIndex(i);
            Instantiate(canvasLevel, transform.position, transform.rotation);
            Debug.Log(level[i]);
        }
    }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
L
LittleBob, 2021-10-28
@Gregory256

Crashes only on stage 0 due to the fact that the script is initialized only on this stage. Try to add the object on which the script hangs to DontDestroyOnLoad. Why it creates two buttons, I don’t know, the script is slightly incomprehensible. And besides, what is the problem of manually placing the buttons in levels. Do you have 1000 of them there?
canvasLevel you throw through the inspector?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question