当我的玩家与门碰撞时,有没有办法加载场景?

发布于 2025-01-10 20:32:02 字数 377 浏览 0 评论 0原文

我正在努力做到这一点,以便当我的玩家获得五分并且他们与门相撞时,我的游戏的菜单屏幕就会出现。我认为这段代码很好,但当我与门碰撞时,主菜单不会显示。谁能帮我解决这个问题吗?下面是我的代码:

    private void OnTriggerEnter(Collider door)
    {
        if(door.tag == "Door" && points == 5)
        {
            SceneManager.LoadScene("MainMenu");
        }
    }
}

我也使用 unityengine.scenemanagement 。包括点在内的其余代码工作正常,只是场景的加载不起作用。

I'm trying to make it so when my player gets five points and they collide with the door, the menu screen for my game shows up. I thought this code was fine but the main menu won't show when I collide with the door. Can anyone help me fix this? Here is my code below:

    private void OnTriggerEnter(Collider door)
    {
        if(door.tag == "Door" && points == 5)
        {
            SceneManager.LoadScene("MainMenu");
        }
    }
}

Im also using unityengine.scenemanagement too. The rest of the code including the points works fine it's just the loading of the scene that won't work.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

捎一片雪花 2025-01-17 20:32:02

有些场景还没有加载到构建设置中,因此从当前场景跳转到其他场景时,无法加载其他场景。您必须将要使用的所有场景加载到构建设置中的萌芽列表中的场景中。方法:加载场景后,单击FILE菜单的构建设置选项,打开构建设置菜单,然后单击菜单中的“添加当前”按钮。您可以看到当前场景已添加到构建设置菜单列表中。以这种方式打开其他场景,并将其添加到UI设置菜单中的萌芽列表中的场景中。添加完所有场景后,保存项目并再次运行,希望对您有帮助

Some scenes have not been loaded into the build settings, so when jumping from the current scene to other scenes, other scenes cannot be loaded. You must load all the scenes you want to use into the scenes in buding list in the build settings. Method: After loading a scene, click the build settings option of the FILE menu, open the build settings menu, and then click the "add current" button in the menu. You can see that the current scenes are added to the build settings menu list. Open other scenes in this way and add them to the scenes in buding list in the uild settings menu. After adding all the scenes, save the project and run it again, I hope it can help you

予囚 2025-01-17 20:32:02

这里发生了四件事,因此您应该检查所有这些事情才能找到问题。

  1. Debug.Log() 放在 OnTriggerEnter 中,以确保它确实被调用
  2. 打印出 door.tag 以确保它匹配“门。”请注意,它区分大小写。
  3. 打印出 points 以确保该值匹配 5。
  4. 仔细检查您的场景名称是否与“MainMenu”匹配。 (编辑:不区分大小写)您还可以通过构建索引加载场景,这样就不会有魔法字符串不匹配的风险。
SceneManager.LoadScene (/* your scene index */);

或者

SceneManager.LoadScene (SceneManager.GetActiveScene ().buildIndex + 1);

场景索引从 0 开始,并根据场景在构建设置中的排列顺序进行计数。可以在“文件”>“文件”中找到。 构建设置

There are four things happening here, so you should check all of them in order to find your problem.

  1. Put a Debug.Log() in OnTriggerEnter to make sure that it's actually being called
  2. Print out door.tag to make sure that it matches "Door." Note that it's case sensitive.
  3. Print out points to make sure that the value matches 5.
  4. Double check that your scene name matches "MainMenu." (EDIT: not case sensitive) You can also load the scene by build index so that there's no risk of mismatching magic strings.
SceneManager.LoadScene (/* your scene index */);

Or

SceneManager.LoadScene (SceneManager.GetActiveScene ().buildIndex + 1);

Scene indexes begin at 0 and count up according to the order the scenes are arranged in your build settings. That can be found in File > Build Settings

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文