在Unity中输入多维数据集时尝试加载新场景 [英] Trying to load a new scene when I enter a cube in Unity

查看:119
本文介绍了在Unity中输入多维数据集时尝试加载新场景的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有以下代码可以进入新场景:

So i have this code to enter a new scene:

using System.Collections;

using UnityEngine;
// add this line to use the SceneManagment library
using UnityEngine.SceneManagement;

public class LoadScenes : MonoBehaviour {

    [SerializeField] private string loadLevel;

    void onTriggerEnter(Collider other) {
        if (other.CompareTag ("Player")) {
            SceneManager.LoadScene (loadLevel);
        }

    }

} 

然后,我将此脚本添加到多维数据集并选择一个触发器.然后,我输入我希望它也发送给我的场景,但是当我走进该场景时,什么也没有发生.我尝试了不同的变体,但似乎不起作用.

I then add this script to the cube and select it a trigger. I then type in the scene that I want it to send me too, but when i walk into it nothing happens at all. I have tried different variations but it just doesnt seem to work.

我正在使用的角色是一个统一资产,称为西装男,但我已将其标签选择为玩家".任何建议将是巨大的!

My character that I am using is a unity asset called man in suit but I have selected its tag as "Player". Any suggestions would be great!

推荐答案

您的触发器的处理程序将不会被调用

Sunimal 都已指出,您需要纠正错字.

The Handler for your trigger won't be invoked

As Sunimal allready noted you need to fix the typo.

  void OnTriggerEnter(Collider other) {
    if (other.CompareTag ("Player")) {
        SceneManager.LoadScene (loadLevel);
    }
}

确保在构建设置中包含并检查了场景

正如您在下面的屏幕快照中看到的那样,我已在我的构建设置中添加了SampleScene.有两种向场景中添加场景的方法

Ensure your Scene is included and checked in the Build Settings

As you can see in the Screenshot below i have added a SampleScene to my build settings. There are 2 ways of adding scenes into the build

  1. 通过单击添加打开的场景",您可以添加以下场景: 当前打开该列表.
  2. 拖动&将场景从ProjectView拖放到列表中
  1. By clicking "Add Open Scenes" you can add the scene which is currently open to that list.
  2. Drag & Drop the scene from your ProjectView into the List

在我的情况下,您的loadLevel字段需要具有值"Scenes/SampleScene".

Your loadLevel field would in my case need to have the value "Scenes/SampleScene".

 [SerializeField] private string loadLevel;

玩家需要一个对撞机

在使用OnTriggerEnter方法时,Player对象需要附加某种Collider.这可以是BoxCollider,SphereCollider或其他某些Collider. 请注意,需要选中触发"复选框.否则它不会触发.

编辑:感谢 Eddge 纠正了我.请参阅答案,以获取有关触发器的更详细说明.

Thanks Eddge for correcting me. See this answer for a deeper explanation about Triggers.

您可以在您的课程中添加 RequireComponent 属性.基本上可以确保您将给定类型添加为组件.添加此脚本时,这还将自动将对撞机添加到对象.

You can add the RequireComponent Attribute at your class. It basically ensures you have the given type added as a component. This will also automatically add a box collider to an object, when you add this script.

[RequireComponent(typeof(BoxCollider))]
public class LoadScenes : MonoBehaviour {
/// your other code is here
}

感谢 Sunimal 以获得此提示!

如果所有这些都无济于事,请提供您的Playerobject的检查器的屏幕截图.这样,我们可以看到哪些组件已附加到该对象上以及如何对其进行配置"

If all this does not help, please provide an screenshot of the inspector of your Playerobject. That way we can see what components are attached to that object and how they are "configured"

这篇关于在Unity中输入多维数据集时尝试加载新场景的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆