- Home /
InGame Menu script resetting/character resetting
When I enter scene, the FirstPersonController from the StandardAssets package works fine. I can use my InGame menu to resume or to return to the main menu. When I return to the main menu and try to go into any scene, the FirstPersonController gets frozen and I have to reset it by clicking escape.
How can i write either a camera check or just a simple few lines to enable and disable that script on load from the menu?
Here is the code im working with:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class MainMenu : MonoBehaviour {
public void LoadLevel1()
{
SceneManager.LoadScene("Hub");
}
public void QuitGame()
{
Application.Quit();
Debug.Log("QUIT");
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityStandardAssets.Characters.FirstPerson;
public class PauseMenu : MonoBehaviour
{
public static bool GameIsPaused = false;
public GameObject PauseMenuUI;
// Start is called before the first frame update
void Start()
{
Cursor.visible = true;
Cursor.lockState = CursorLockMode.None;
}
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown(KeyCode.Escape))
{
if (GameIsPaused)
{
Resume();
} else
{
Pause();
}
}
}
public void Resume ()
{
PauseMenuUI.SetActive(false);
Time.timeScale = 1f;
GameIsPaused = false;
GameObject.Find("FPSController").GetComponent<FirstPersonController>().enabled = true;
Cursor.visible = false;
Cursor.lockState = CursorLockMode.Locked;
}
public void Pause ()
{
PauseMenuUI.SetActive(true);
Time.timeScale = 0f;
GameIsPaused = true;
GameObject.Find("FPSController").GetComponent<FirstPersonController>().enabled = false;
Cursor.visible = true;
Cursor.lockState = CursorLockMode.None;
}
public void LoadMenu()
{
SceneManager.LoadScene("MainMenu");
}
public void OptionsMenu()
{
}
}
what do you mean by enable and disable that script? to call the method pause or to stop update or something i am not getting?
it's in the code. he's toggling the FPSController component, so he thought issue might be somewhere there. it's not. the issue is him setting timescale to 0 and not setting it back.
Not sure if I get it right, but maybe you are setting timescale to 0 before going to main menu and not setting it back to normal.
Answer by sh_code · Jan 17, 2019 at 01:32 PM
you are in the game
to get to main menu, you press Escape, which shows the Pause menu, which calls the Pause() function, which sets timeScale to 0.
Then you click load main menu, which switches the scene. timeScale is still 0, because it's global. You don't notice it, because by default, UI elements are not affected by it.
Then you go to any level, timeScale is still 0, and the first thing you can notice it on, is your player, which IS affected by it (unlike the UI elements)
Then, to unfreeze him, you press escape, which brings up the pause menu, which does the pause thing. And then YOU CLOSE THE PAUSE MENU, WHICH, FINALLY, FOR THE FIRST TIME, SETS THE timeScale BACK TO 1, SO YOUR CHARACTER UNFREEZES.
Again, Time.timeScale is global, persists across scenes. To solve this, either set timeScale to 1 before loading the main menu scene, or set timeScale to 1 first thing in the main menu scene somewhere.