How to pause menu

hello, I’m learning C# and unity scripting and while learning I’m trying to make some things in Unity… Well my objective is to not use any of the created assets, I want to know how to script ALL.

So, Instead of downloading a simple pause menu asset I’m creating my own and I found a problem. I have a script called PauseMenu, in which I have a Canvas prefab “pauseMenuPrefab” and a (for now) one button, the Continue button to resume gameplay.
Here the script:
Code

[SerializeField]
	GameObject pauseMenuPrefab;
	private GameObject pauseMenuInstance;

	public Button continueGame;
	public bool isGamePaused;

	// Use this for initialization
	void Start()
	{
		continueGame = continueGame.GetComponent<Button>();
	}

	// Update is called once per frame
	void Update()
	{
		CheckForInput();
		ShouldBePaused();
	}

	public void TogglePauseState()
	{
		isGamePaused = !isGamePaused;
	}

	void CheckForInput()
	{
		if (Input.GetKeyDown(KeyCode.Escape))
		{
			TogglePauseState();
		}
	}

	void ShouldBePaused()
	{
		if (isGamePaused)
		{
			//Create Pause Menu
			pauseMenuInstance = Instantiate(pauseMenuPrefab);
			pauseMenuInstance.name = pauseMenuPrefab.name;

			//Pause time and write log:
			Time.timeScale = 0;
			Debug.Log("Game has been paused!");
		}
		else
		{
			Destroy(pauseMenuInstance);
			Time.timeScale = 1;
			Debug.Log("Game has been unpaused!");
		}
	}

So, the problem now is how I can refer (In the inspector) a button that is not yet created BUT in the pause menu prefab?

Thanks for the help,
Erwin.

Because of the way Unity works, certain setting in prefabs will be removed upon instantiation. Now setting values after the fact can be done but in the case of setting an event after instantiation is different.

Assuming what you are looking to do is instantiate a pause menu when you hit the ESC key then destroy the pause menu when either the ESC key or the Continue button is is pressed, you can do it like this

[SerializeField]
GameObject pauseMenuPrefab;

public Button continueGame;
public bool isGamePaused;

// Use this for initialization
void Start()
{
    // continueGame = continueGame.GetComponent<Button>();  // this does not make sense
}

void Update()
{
    if (Input.GetKeyDown(KeyCode.Escape))
    {
        CheckForInput();
    }
}

public void TogglePauseState()
{
    isGamePaused = !isGamePaused;
    if (isGamePaused)
    {
        //Create Pause Menu
        pauseMenuInstance = Instantiate(pauseMenuPrefab);
        pauseMenuInstance.name = pauseMenuPrefab.name;

        Button[] buttons = pauseMenuInstance.GetComponentsInChildren<Button>();
        if (buttons.Length > 0)
        {
            continueGame = buttons[0]; // get the very first button in array
            // if there are more than one button involve then you may want to search for button by name like this
            // for (int i = 0; i < buttons.Length; i++)
            // {
            //     if (buttons*.name == "MyButtonName")*

// {
// continueGame = buttons*;*
// break;
// }
// }
if (continueGame != null)
{
continueGame.onClick.AddListener(() => CheckForInput());
}
}

//Pause time and write log:
Time.timeScale = 0;
Debug.Log(“Game has been paused!”);
}
else
{
Destroy(pauseMenuInstance);
continueGame = null;
Time.timeScale = 1;
Debug.Log(“Game has been unpaused!”);
}
}

void CheckForInput()
{
TogglePauseState();
}
Assuming you will be adding more than one button this can be handled in the for loop I provided (more code needed).
Of course the way I prefer to do this is to set up my pause menu and save a prefab like you have. But leave/add the pause menu to each scene. I can easily set up all buttons in each scene. For enabling/disabling of the pause menu I would have a reference do something like this
public Canvas pauseMenu;
void CheckForInput(bool enabled)
{
if (pauseMenu != null)
{
pauseMenu.enable = enabled;
}
}