-1
\$\begingroup\$

I already understand how to do this but the problem I'm having is:

When I go from "MainMenu" to "Level01" and then back, the buttons lose their reference. How can I fix this?

using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
public class LevelManager : MonoBehaviour
{
    private static LevelManager Instance;
    public static int CurrentScene;

    private void Start()
    {
        if(Instance != null)
        {
            Destroy(gameObject);
        }
        else
        {
            DontDestroyOnLoad(gameObject);
            Instance = this;
        }
        //LevelManagerStartup();
    }

    private void Update()
    {
        if(Input.GetKeyDown(KeyCode.R))
        {
            if(CurrentScene != 0)
            {
                LoadScene(CurrentScene);
            }
        }

        if(Input.GetKeyDown(KeyCode.M))
        {
            if(CurrentScene != 0)
            {
                LoadScene(0);
            }
        }
    }

    public void LoadScene(int BuildIndex)
    {
        CurrentScene = BuildIndex;
        SceneManager.LoadScene(BuildIndex);
        if(CurrentScene == 0)
        {
            LevelManagerStartup();
        }
    }

    private void LevelManagerStartup()
    {
        //get a refarance to the highscoer manager
        Debug.Log(CurrentScene);
        if(CurrentScene == 0)
        {
            Cursor.lockState = CursorLockMode.None;
            Cursor.visible = true;
            RefButtons();
        }
        else
        {
            Cursor.lockState = CursorLockMode.Locked;
            Cursor.visible = false;
        }
        //make sure this stuff working.
        Debug.Log("LevelManager");
    }

    private void RefButtons()
    {
        GameObject ButtonHolder = GameObject.Find("ButtonHolder");
        Button[] buttons = new Button[3];
        for(int i = 0; i < ButtonHolder.transform.childCount; i++)
        {
            buttons[i] = ButtonHolder.transform.GetChild(i).GetComponent<Button>();
            buttons[i].onClick.AddListener(() => LoadScene(i));
        }
    }
}
\$\endgroup\$
  • 1
    \$\begingroup\$ What specific reference are you losing? Walk us through how you've set up your project and the specific symptoms of the problem you're observing. \$\endgroup\$ – DMGregory Nov 9 at 19:26
  • \$\begingroup\$ Yes the LevelManager is an instantance in the MainScene and it is neither DoNotDestroyOnLoad nor does it get deleted on load. but i'm using a singleton pattern, how that works is when the Start is called then a new copy is made and replaces the old LevelManager instance. Therefor the reference is lost, i don`t know why this happens. Hope this is clear Thanks for response. \$\endgroup\$ – Dan-mechanics Nov 11 at 18:11
  • \$\begingroup\$ I have found a solution thank you for your help and time. \$\endgroup\$ – Dan-mechanics Nov 12 at 18:53
  • \$\begingroup\$ Please post your solution as an answer that can help future users. \$\endgroup\$ – DMGregory Nov 12 at 18:54
-1
\$\begingroup\$

To solve I made everything static.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.