Hello.
I’m trying to code a simple script that allows me to call menus without coding a script for every menu. For doing that, I’m using a variable that stores the name of the menu that I want to call.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Menus : MonoBehaviour
{
public static bool GamePaused = false;
private string menu = "";
// Update is called once per frame
void Update()
{
if(Input.GetKeyDown(KeyCode.Escape))
{
menu = "PauseMenu";
checkPause();
}
else if(Input.GetKeyDown(KeyCode.LeftShift))
{
menu = "SwitchMenu";
checkPause();
}
}
void checkPause()
{
if(GamePaused)
{
Resume();
} else {
Pause();
}
}
public void Resume()
{
GameObject.Find(menu).SetActive(false);
Time.timeScale = 1f;
GamePaused = false;
}
public void Pause()
{
GameObject.Find(menu).SetActive(true);
Time.timeScale = 0f;
GamePaused = true;
}
}
Thank you for reading!