Hey, so I need to know how to make a script, to activate another script (script-ception?).
The idea is that when the player presses tab, another script (which is toggled off), gets toggled On. Later, when the player hits tab again, the script deactivates. (It’s a pause menu, if you have’t already figured it out.)
It’d be cool if we could make the script a variable that I can change if I wanted to have multiple scripts.
Thanks
Okay, lets make it simple ok?
C#:
public bool isPaused = false;
void Update(){
if(Input.GetButton(KeyCode.Tab){
PauseGame();
}
}
public void PauseGame(){
isPaused = !isPaused;
//some more velocity/force pause modes.
}
I think this will do what you want.
public bool isPaused = false;
void Update()
{
if (Input.GetButtonDown(KeyCode.Tab)&&!isPaused)
{
isPaused = true;
gameObject.GetComponent<Script>().enabled = true;
}
if (Input.GetButtonDown(KeyCode.Tab)&&isPaused)
{
isPaused = false;
gameObject.GetComponent<Script>().enabled = false;
}
}
A more simple approach, have it all in one script?
void Update()
{
if(Input.GetKeyDown(KeyCode.P))
{
pauseMenu = true;
Time.timeScale = 0; //this sets the time to 0 and pauses the game
}
}
void OnGUI()
{
if (pauseMenu)
{
if (GUI.Button (new Rect (0,0,20,20), "Return to Game"))
{
pauseMenu = false;
Time.timeScale = 1; //this continues the game
}
}