i cant make this pause menu work in unity - c#

im trying to follow this video
and i think i did everything right but its not doing anything then i click the escape button
using System.Collections.Generic;
using UnityEngine;
public class PauseMenu : MonoBehaviour
{
public static bool GameIsPaused = false;
public GameObject pauseMenuUI;
// 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;
}
void Pause()
{
pauseMenuUI.SetActive(true);
Time.timeScale = 0f;
GameIsPaused = true;
}
public void LoadMenu()
{
Debug.Log("loading menu");
}
public void QuitGame()
{
Debug.Log("quiting game");
}
}
this is how the panel looks like
it was before in Canvas but i moved it because i was trying to make it work but it didnt help
someone knows maybe how to fix it?

Related

Why wont my Pause screen come up when i click esc (in Unity)

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class PauseMenu : MonoBehaviour
{
public static bool gameIsPaused = false;
public GameObject PauseMenuUI;
void Update()
{
if (Input.GetKeyDown(KeyCode.Escape))
{
if (gameIsPaused)
{
Resume();
} else
{
Pause();
}
}
Debug.Log("KeyInput");
}
public void Resume ()
{
PauseMenuUI.SetActive(false);
Time.timeScale = 1f;
gameIsPaused = false;
}
void Pause ()
{
PauseMenuUI.SetActive(true);
Time.timeScale = 0f;
gameIsPaused = true;
}
public void LoadMenu()
{
Debug.Log("Loading Menu...");
Time.timeScale = 1f;
SceneManager.LoadScene ("MainMenu");
}
public void QuitGame()
{
Debug.Log ("Quitting Game");
Application.Quit();
}
}
My code wont open the actual pause screen is there any reason why
i have tried editing it in different ways and i also tried some other code online but it never seems to work for me i have also went onto the unity forms and that wasn't really helpful

How do I fix my code for blocking showing/hiding menu?

I got problem with pause menu holding esc key fix.
I writed simple script that blocks showing/hiding pause menu for 5 seconds, but it doesn't work.
Can someone tell me what I'm doing wrong or fix my code?
(Here I'm writing cuz i cant post my question lol)
Here is code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MenuController : MonoBehaviour
{
public static bool IsGamePaused = false;
public static bool IsClickingBlocked = false;
public GameObject zagadka1;
// Update is called once per frame
void Update()
{
if (Input.GetKey(KeyCode.T))
{
if (IsGamePaused)
{
if (IsClickingBlocked == false) {
ClickStopCounter();
zagadka1Start();
}
}
else
{
if (IsClickingBlocked == false)
{
zagadka1Stop();
ClickStopCounter();
}
}
}
}
IEnumerator ClickStopCounter()
{
IsClickingBlocked = true;
yield return new WaitForSeconds(5);
IsClickingBlocked = false;
}
public void zagadka1Start()
{
zagadka1.SetActive(false);
Time.timeScale = 1f;
IsGamePaused = false;
Cursor.lockState = CursorLockMode.Locked;
}
void zagadka1Stop()
{
zagadka1.SetActive(true);
Time.timeScale = 0f;
IsGamePaused = true;
Cursor.lockState = CursorLockMode.None;
}
You should use StartCoroutine when calling the Coroutine.
Try this one. StartCoroutine(ClickStopCounter())

How to keep player skin through restart

I'm trying to keep the player skin, even when reloading the scene, or moving on to a new one. The player object changes every scene.
The player skin is chosen in a pause menu, with three buttons. Each one of those buttons calls a function in the script below. I'm trying to call one of these functions, based on what value the PlayerPrefs int holds, and the function does get called; But throws the error MissingReferenceException: The object of type 'GameObject' has been destroyed but you are still trying to access it
Below is what i have already tried, but this throws an error on scene reload (death)
i don't know what i'm doing wrong here.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class Pausemenu : MonoBehaviour
{
public static bool gameIsPaused = false;
public GameObject pauseMenuUI;
public Material BelgianMat;
public Material Ball2;
public Material Rainbow;
public GameObject Player;
// 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;
}
void Pause() {
pauseMenuUI.SetActive(true);
Time.timeScale = 0f;
gameIsPaused = true;
}
public void LoadMenu() {
Time.timeScale = 1f;
gameIsPaused = false;
SceneManager.LoadScene("Menu");
}
public void QuitGame() {
Debug.Log("Quitting");
Application.Quit();
}
public void ApplyBelgian() {
Player.GetComponent<Renderer>().material = BelgianMat;
PlayerPrefs.SetInt("playerMat", 0);
}
public void ApplyBall2() {
Player.GetComponent<Renderer>().material = Ball2;
Debug.Log("Applied ball two");
PlayerPrefs.SetInt("playerMat", 1);
}
public void ApplyRainbow() {
Player.GetComponent<Renderer>().material = Rainbow;
PlayerPrefs.SetInt("playerMat", 2);
}
void OnEnable()
{
Debug.Log("OnEnable called");
SceneManager.sceneLoaded += OnSceneLoaded;
}
// called second
void OnSceneLoaded(Scene scene, LoadSceneMode mode)
{
Debug.Log("OnSceneLoaded: " + scene.name);
Debug.Log(mode);
if (PlayerPrefs.GetInt("playerMat") == 0) {
ApplyBelgian();
}
else if (PlayerPrefs.GetInt("playerMat") == 1) {
Debug.Log("gonna apply ball 2");
ApplyBall2();
}
else if (PlayerPrefs.GetInt("playerMat") == 2) {
ApplyRainbow();
}
}
}
I don't know why but it seems like the reference to the Player object may be broken after loading the scene. If this is the case, add a "Player" tag to your Player object and add this to the OnEnable function: Player = GameObject.FindWithTag("Player");

PauseMenu - FPS Camera still moves when in PauseMenu

Whenever I pause everything stops, that's how I want it to be, but the only thing that does not stop is the camera. I move my cursor the camera still follows. I want it to freeze. I am using the FPS Controller Asset in the asset store
using System.Collections;
using System.Collections.Generic;
using UnityStandardAssets.Characters.FirstPerson;
using UnityEngine;
using UnityEngine.SceneManagement;
public class PauseMenu : MonoBehaviour
{
public static bool GameIsPaused = false;
static bool IsPaused = false;
public GameObject pauseMenuUI;
// 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;
AudioListener.pause = false;
GameIsPaused = false;
}
void Pause()
{
pauseMenuUI.SetActive(true);
Time.timeScale = 0f;
AudioListener.pause = true;
GameIsPaused = true;
}
public void LoadMenu()
{
Time.timeScale = 1f;
SceneManager.LoadScene("Menu");
}
public void QuitGame()
{
Debug.Log("Exiting Game");
Application.Quit();
}
}
Get the reference to the Character Controller Script and then disable it:
mycharfpscon.enabled = false;
And then enabble it back:
mycharfpscon.enabled = true;

Don't know how to use SetActive() properly

So, my script that I've written isn't working fully. I have A pause button and when I press it, it triggers my bool and shows that its working properly but when I'm "Paused" my UI doesn't pop up and my game doesn't stop in the back ground. I hope you can understand clearly. I am a beginner!
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class PauseManager : MonoBehaviour {
public GameObject pauseMenu;
public bool paused = false;
public void start()
{
pauseMenu.SetActive(false);
}
public void update()
{
if(Input.GetButtonDown("escape"))
{
paused = !paused;
}
if (paused)
{
pauseMenu.SetActive(true);
Time.timeScale = 0;
}
if (!paused)
{
pauseMenu.SetActive(false);
Time.timeScale = 1;
}
}
public void Resume()
{
paused = false;
}
public void pauseButton()
{
paused = true;
}
}
Advice you change your code to this:
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class PauseManager : MonoBehaviour {
public GameObject pauseMenu;
bool paused = false;
void Start()
{
pauseMenu.SetActive(false);
}
void Update()
{
if(Input.GetButtonDown("escape"))
{
paused = !paused;
}
if (paused)
{
PauseGame();
}
if (!paused)
{
ResumeGame();
}
}
void PauseGame(){
pauseMenu.SetActive(true);
Time.timeScale = 0f;
}
void ResumeGame(){
pauseMenu.SetActive(false);
Time.timeScale = 1f;
}
public void Resume()
{
ResumeGame();
}
public void pauseButton()
{
PauseGame();
}
}
I think the problem is on that when you pause button pressed, timescale be 0, and update does not work.

Categories

Resources