Search Unity

Change scene

Discussion in 'Scripting' started by Timoty75, Aug 18, 2017.

  1. Timoty75

    Timoty75

    Joined:
    May 29, 2017
    Posts:
    150
    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?
     
  2. MattM_Unity

    MattM_Unity

    Unity Technologies

    Joined:
    Aug 18, 2017
    Posts:
    45
    Hello Timoty!

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

    Code (CSharp):
    1. using UnityEngine.SceneManagement;
    Then, the function that allows you to switch from one scene to another is this one:

    Code (CSharp):
    1. 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:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.SceneManagement;
    5.  
    6. public class loadRoomScene : MonoBehaviour
    7. {
    8.     private void OnTriggerEnter (Collider sceneTrigger)
    9.     {
    10.         //When your Character collides with the box collider
    11.         if (sceneTrigger.CompareTag("TagOfTheColliderThatWillTriggerTheLoading"))
    12.         {
    13.             SceneManager.LoadScene("NameOfYourScene", LoadSceneMode.Additive);
    14.         }
    15.     }
    16. }
    The other mode is LoadSceneMode.Single. This mode will close all current loaded scenes and load the new one.
     
    Last edited: Aug 18, 2017
  3. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Hello Matt @ post #1. :)

    Hi Timoty, good luck with your game :)
     
    Timoty75 likes this.
  4. Timoty75

    Timoty75

    Joined:
    May 29, 2017
    Posts:
    150
    Grazie mille Matt ... lo provo !!
    Savino
     
  5. TheWittyRobin

    TheWittyRobin

    Joined:
    Aug 18, 2018
    Posts:
    36
    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?
     
  6. Timoty75

    Timoty75

    Joined:
    May 29, 2017
    Posts:
    150
    Thanks a lot to all..I resolve it!
     
  7. unity_kvm3cLicJ8qpig

    unity_kvm3cLicJ8qpig

    Joined:
    Sep 9, 2018
    Posts:
    1
    Hello, where is the Tag of collider in unity please ?
     
  8. TheWittyRobin

    TheWittyRobin

    Joined:
    Aug 18, 2018
    Posts:
    36
    what do you mean? like how do you mark it as a trigger?
     
  9. Timoty75

    Timoty75

    Joined:
    May 29, 2017
    Posts:
    150
    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