为什么我无法让 C# 脚本在 Unity 中工作

问题描述 投票:0回答:1

所以我从统一开始,只是为简历菜单编写了代码,但我不知道如何使其有效

这是代码:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ResumeMenu : MonoBehaviour
{
    public GameObject pauseMenu;

    public bool isPaused;

    void Start()
    {
        pauseMenu.SetActive(false);
    }

    void Update()
    {
        if (Input.GetKeyDown(KeyCode.Escape))
        {
            if (isPaused)
            {
                ResumeGame();
            }
            else
            {
                PauseGame();
            }
        }
    }
    public void PauseGame()
    {
        pauseMenu.SetActive(true);
        Time.timeScale = 0f;
        isPaused = true;
    }
    public void ResumeGame()
    {
        pauseMenu.SetActive(false);
        Time.timeScale = 1f;
        isPaused = false;
    }
}

this is how the game looks rn

我不知道把脚本放在哪里,所以菜单只能在游戏中使用,我尝试看一些视频,大多数人都使用一些游戏管理器

c# unity-game-engine game-development unityscript
1个回答
0
投票

你的代码没有问题。 在场景中创建一个空对象并在其中插入脚本。如果您将“pauseMenu”连接到您的脚本,它应该可以正常工作。

如果你想让它像按钮一样工作,你也可以将下面的代码连接到按钮。

© www.soinside.com 2019 - 2024. All rights reserved.