首页 > 解决方案 > SetActive 无法识别?

问题描述

所以是的,我正在尝试制作一个统一的小游戏,创建一个暂停菜单,当我创建一个带有 a 的变量时GameObject,我不能使用SetActive它,它基本上说它SetActive无法识别。

这是代码:

bool IsPaused;
GameObject[] Pause_Menu;

// Start is called before the first frame update
void Start()
{
    IsPaused = false;
    Pause_Menu = GameObject.FindGameObjectsWithTag("Pause_Menu");
}

// Update is called once per frame
void Update()
{
    Pause_Menu.SetActive(IsPaused);

    if (Input.GetKeyDown("escape"))
    {
        IsPaused = true;
    }
    if ((Input.GetKeyDown("escape")) && (IsPaused = true))
    {
        IsPaused = false;
    }
} 

标签: unity3d

解决方案


当您使用FindGameObjectsWithTag.

你可能想要的是这样的:

bool IsPaused;
GameObject Pause_Menu;

// Start is called before the first frame update
void Start ( )
{
    IsPaused = false;
    Pause_Menu = GameObject.FindGameObjectWithTag ( "Pause_Menu" );
}

// Update is called once per frame
void Update ( )
{
    Pause_Menu.SetActive ( IsPaused );

    if ( Input.GetKeyDown ( "escape" ) )
    {
        IsPaused = true;
    }
    if ( ( Input.GetKeyDown ( "escape" ) ) && ( IsPaused = true ) )
    {
        IsPaused = false;
    }
}

将 Pause_Menu 定义为单个游戏对象,而不是游戏对象数组,然后更改FindGameObjectsWithTagFindGameObjectWithTag(删除“s”)。甚至更短的版本FindWithTag,如:

Pause_Menu = GameObject.FindWithTag ( "Pause_Menu" );

这是FindWithTag的 Unity 文档。


推荐阅读