Home > Enterprise >  Is there any problems in my pause menu system?
Is there any problems in my pause menu system?

Time:08-20

I am trying to make a pause menu system, but it doesn't work. It has to pop up when I press the Escape key, and it doesn't show any error messages.

the code is:

using System.Threading;
using System;
using System.Net.Mime;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class pauseMenu : MonoBehaviour
{

    public static bool paused = false;
    public GameObject Background;

    void Start()
    {
        Background.SetActive(false);
    }
    void FixedUpdate()
    {
        if (Input.GetKeyDown(KeyCode.Escape))
        {
            if (paused)
            {
                Background.SetActive(false);
                Time.timeScale = 1f;
                paused = false;
            }
            else
            {
                Background.SetActive(true);
                Time.timeScale = 0f;
                paused = true;
            }
        }
    }

I know this is a straightforward question, but I'm an absolute beginner. So I need your help.

CodePudding user response:

Codes in FixedUpdate are supposed to be in sync with the physics system. Based on your framerate and time settings, it might not be called even once per some frames. Therefore, Input.GetKeyDown(..), which is a one-time event, might not be caught in FixedUpdate. So, I would use Update, which is called once per frame.
Also, you've set the timeScale to zero right after enabling the pause menu. Beware that animations are affected by the time scale by default. If you have any animation for popping up the pause menu, you need to mark it as unscaled via the inspector (Select the Animator and set Update Mode to Unscaled Time)

  • Related