首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >统一,如何在其他场景中解除/激活组件

统一,如何在其他场景中解除/激活组件
EN

Stack Overflow用户
提问于 2019-05-30 15:55:22
回答 1查看 454关注 0票数 0

我的问题如下。

我在主菜单场景中,有两个按钮:一个是playerVSplayer,另一个是VScpu。

现在,我希望启用/禁用场景中的某些组件,当按下其中一个按钮时,这些组件将被加载。

我该如何解决这个问题?

代码语言:javascript
复制
public void PlayerVsPlayer()
{
    GameObject[] objs = SceneManager.GetSceneByName("First Level").GetRootGameObjects();
    foreach(GameObject o in objs)
    {
        if(o.gameObject.tag == "--Gameplay")
        {
            o.gameObject.GetComponentInChildren<StaticPlayerCtr_p2>().enabled = true;
            o.gameObject.GetComponentInChildren<AI>().enabled = false;
            o.gameObject.GetComponentInChildren<UnityEngine.AI.NavMeshAgent>().enabled = false; 
        }
    }
    SceneManager.LoadScene("First Level");
}

public void VsCPU()
{
    GameObject[] objs = SceneManager.GetSceneByName("First Level").GetRootGameObjects();
    foreach (GameObject o in objs)
    {
        if (o.gameObject.tag == "--Gameplay")
        {
            o.gameObject.GetComponentInChildren<StaticPlayerCtr_p2>().enabled = false;
            o.gameObject.GetComponentInChildren<AI>().enabled = true;
            o.gameObject.GetComponentInChildren<UnityEngine.AI.NavMeshAgent>().enabled = true;
        }
    }
    SceneManager.LoadScene("First Level");
}

public void QuitGame()
{
    Application.Quit();
}

这就是错误:

ArgumentException:场景无效。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-05-31 03:15:05

据我所知,您无法从其他场景访问场景GameObjects。

如果您希望有两个不同的场景,取决于一个选项(这里是PvP & PvCpu),那么您必须创建两个不同的场景。然后根据选择的选项,假设PvP和PvCpu是按钮,然后根据按下的按钮加载所需的场景。

希望我能帮你。祝你的比赛好运!

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56381633

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档