简体   繁体   English

在Unity中输入多维数据集时尝试加载新场景

[英]Trying to load a new scene when I enter a cube in 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! 任何建议都很好!

The Handler for your trigger won't be invoked 您的触发器的处理程序将不会被调用

As Sunimal allready noted you need to fix the typo. 正如Sunimal早已指出的那样,您需要纠正错字。

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

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. 正如您在下面的屏幕截图中所看到的,我已经在我的构建设置中添加了SampleScene。 There are 2 ways of adding scenes into the build 有两种向场景中添加场景的方法

  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 将场景从ProjectView拖放到列表中

构建设置的屏幕截图

Ensure your SceneName is set correctly 确保您的SceneName设置正确

Your loadLevel field would in my case need to have the value "Scenes/SampleScene". 在我的情况下,您的loadLevel字段需要具有“ Scenes / SampleScene”值。

 [SerializeField] private string loadLevel;

The player needs a collider 玩家需要一个对撞机

As you use the OnTriggerEnter method, your Player object needs to have some sort of Collider attached to it. 使用OnTriggerEnter方法时,Player对象需要附加某种Collider。 This can be a BoxCollider, SphereCollider or some other Collider. 这可以是BoxCollider,SphereCollider或其他某些Collider。 Note that the "Is Trigger" checkbox needs to be checked. 请注意,需要选中“触发”复选框。 Else it won't act as trigger. 否则,它将不会触发。

Edit: Thanks Eddge for correcting me. 编辑:感谢Eddge纠正我。 See this answer for a deeper explanation about Triggers. 有关触发器的更详细说明,请参见此答案。

对撞机组件的图像

Programatically ensure you have a BoxCollider component beside your LoadScenes component 以编程方式确保在LoadScenes组件旁边有一个BoxCollider组件

You can add the RequireComponent Attribute at your class. 您可以在类中添加RequireComponent属性。 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
}

Thanks to Sunimal for this hint! 感谢Sunimal的提示!

What if that did not solve the problem? 如果那不能解决问题怎么办?

If all this does not help, please provide an screenshot of the inspector of your Playerobject. 如果所有这些都无济于事,请提供PlayerObject检查器的屏幕快照。 That way we can see what components are attached to that object and how they are "configured" 这样,我们可以看到哪些组件已附加到该对象以及如何对其进行“配置”

SceneManagement SceneManagement

To use the SceneManager to load a scene you must ensure that your scene is in the build settings, per Tobias's answer. 要使用SceneManager加载场景,必须按照Tobias的答案确保场景位于构建设置中。

Triggers 触发器

In all software development case does matter and it is incredibly important. 在所有软件开发中,案例确实很重要,这非常重要。 OnTriggerEnter is not the same as onTriggerEnter , also note OnTriggerEnter(Collider col) is not the same as OnTriggerEnter(Collision col) OnTriggerEnteronTriggerEnter ,还请注意OnTriggerEnter(Collider col)OnTriggerEnter(Collision col)

In order to use any of the trigger methods there are 3 things that are a must: 为了使用任何触发方法,必须满足以下三个条件:

  1. Both Objects have to have colliders. 两个对象都必须具有对撞机。
  2. One of the colliders have to be marked as a trigger. 对撞机之一必须标记为触发器。
  3. One of the objects have to have a rigidbody. 物体之一必须具有刚体。

The trigger event is sent to the object with the rigidbody and whatever object is the trigger, in the circumstance that both objects are triggers both will receive it. 触发事件随刚体一起发送到对象,无论对象是触发器,在两个对象都是触发器的情况下,两个都将接收它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM