Change scene

Gentle Guys,
I have a problem:

I should change the scene to the character by crossing an invisible collider. Example: the character will change the scene by opening the door and when it crosses the invisible cube, the scene two appears. Can anyone tell me where I can find a complete script about this?

Hello Timoty!

First of all, you would need to add the library that handles the scene management at the top of your script.

using UnityEngine.SceneManagement;

Then, the function that allows you to switch from one scene to another is this one:

SceneManager.LoadScene("NameOfYourScene");

Before loading your scene, remember to add it in the BuildSettings: File > Build Settings then drop all the scenes you want to load in the window Scenes in Build.

You can also add a LoadSceneMode after the name of your scene: you have the LoadSceneMode.Additive that will add the scene you want to load to your current loaded scene (let’s say, you’re in your house, you want to go in your room. The room itself is in a scene of its own, but it’s not loaded yet). A script that does that could look like this:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;

public class loadRoomScene : MonoBehaviour
{
    private void OnTriggerEnter (Collider sceneTrigger)
    {
        //When your Character collides with the box collider
        if (sceneTrigger.CompareTag("TagOfTheColliderThatWillTriggerTheLoading"))
        {
            SceneManager.LoadScene("NameOfYourScene", LoadSceneMode.Additive);
        }
    }
}

The other mode is LoadSceneMode.Single. This mode will close all current loaded scenes and load the new one.

3 Likes

Hello Matt @ post #1. :slight_smile:

Hi Timoty, good luck with your game :slight_smile:

1 Like

Grazie mille Matt … lo provo !!
Savino

Ive been looking for this for hours thank you!!!

What if you plan on using it as a door to walk inside then back outside?

Thanks a lot to all…I resolve it!

Hello, where is the Tag of collider in unity please ?

what do you mean? like how do you mark it as a trigger?

I would change animation on keypress: if my gameobject(sword) is active, key play one animation but if the sword is deactivate, the same keypress plays another animation. How can I do? Thank a lot