简体   繁体   中英

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.

  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. 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

构建设置的屏幕截图

Ensure your SceneName is set correctly

Your loadLevel field would in my case need to have the value "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. This can be a BoxCollider, SphereCollider or some other Collider. Note that the "Is Trigger" checkbox needs to be checked. Else it won't act as trigger.

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

对撞机组件的图像

Programatically ensure you have a BoxCollider component beside your LoadScenes component

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
}

Thanks to Sunimal for this hint!

What if that did not solve the problem?

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"

SceneManagement

To use the SceneManager to load a scene you must ensure that your scene is in the build settings, per Tobias's answer.

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)

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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