Search Unity

Change Scene/Level on Trigger

Discussion in 'Community Learning & Teaching' started by Traxxy, Apr 22, 2016.

  1. Traxxy

    Traxxy

    Joined:
    Mar 21, 2016
    Posts:
    18
    first a few checks,
    make sure you have your player tagged as "Player"
    make sure you have all scenes in build settings, add all scenes
    save your entire project

    The Trigger to Change Scene
    create a plane/sphere/cube something to act as the trigger and select is trigger
    • create a C# script called something like LevelManager or SceneManager, attach it to your trigger object and open in MonoDevelop or VS
    • clear "void Start()" and "void Update" as they are not needed and create "void OnTriggerEnter(Collider ChangeScene)" and the { }
    • in this function we type if( ChangeScene.gameObject.CompareTag("Player")) and open { } <-- them
    you don't have to put Collider ChangeScene you can use whatever is easiest for you, it doesn't matter as long as you have your Collider or Collider2D the ChangeScene is the name you choose it to be called

    The if statement tells your trigger object, if the player hits you change the scene, ight trigger? and trigger says ight man no problem I got your back. They cool with eachother like that.

    and CompareTag("Player") is your player that should already be tagged as player. if its not youll get an error.

    • in the if statement we type "Application.LoadLevelAdditive(1);"
    Application is your application.. obviously right..? and the int or the number in the brackets is the scene number in your build settings. remember I said a few checks? that's why or else the level wont load. the on can be replaced with 2, 3, 4, 5 etc.. its the scene you want loaded.

    Here is a code sample to see for yourself ight,
    Code (CSharp):
    1. void OnTriggerEnter(Collider ChangeScene) // can be Collider HardDick if you want.. I'm not judging you
    2. {
    3.     if(ChangeScene.gameObject.CompareTag("Player"))
    4.     {
    5.         Application.LoadLevelAdditive(1); //1 is the build order it could be 1065 for you if you have that many scenes
    6.     }
    7. }
    8.  
    9. //Traxxy Out
     
    Last edited: Apr 22, 2016
  2. BryanO

    BryanO

    Joined:
    Jan 8, 2014
    Posts:
    186
    How do we avoid multiple loads of the additive scene if the player walks back through the trigger?
     
  3. ReZult_Studios

    ReZult_Studios

    Joined:
    Jun 11, 2016
    Posts:
    29
    Add a destroy game object
     
  4. Mrawesome109

    Mrawesome109

    Joined:
    Jan 7, 2018
    Posts:
    1
    :)thank you
     
  5. Lorbie

    Lorbie

    Joined:
    Aug 13, 2018
    Posts:
    1
    i m french and your script does not work can you help me please
     
    TheWittyRobin likes this.
  6. TheWittyRobin

    TheWittyRobin

    Joined:
    Aug 18, 2018
    Posts:
    36
    its really old they don't use the same directories any more
     
  7. boxuanliao

    boxuanliao

    Joined:
    Sep 22, 2018
    Posts:
    1
    kjhgfdddddss
     
  8. JackB226

    JackB226

    Joined:
    Jul 29, 2019
    Posts:
    1
    doesn't work for me. i have tried so many videos and tutorials to get it working but it doesn't want to
     
  9. GOLDY00

    GOLDY00

    Joined:
    Mar 16, 2013
    Posts:
    139
    Hey bro I was wondering how do I make it so same script different triggers go to diff scenes do I duplicate the scripts and change the number
     
  10. nilevansiu

    nilevansiu

    Joined:
    Mar 15, 2020
    Posts:
    1
    using UnityEngine.SceneManagement;

    public class ChangeScene : MonoBehaviour
    {
    private void OnTriggerEnter(Collider other)
    {
    SceneManager.LoadScene("SceneName");
    }​
    }

    //The collider must click IsTrigger, so that when Player pass there can trigger and change scene
    //Is it work for u guys?
     
  11. iSoloXIV

    iSoloXIV

    Joined:
    Nov 28, 2020
    Posts:
    1
    Here is my code
    Code (CSharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5. using UnityEngine.SceneManagement;
    6.  
    7. public class gotonextstahe : MonoBehaviour
    8. {
    9.     private void OnTriggerEnter(Collider other)
    10.     {
    11.         SceneManager.LoadScene("Stage02");
    12.     }
    13. }
    14.  
    And it works with me just find.
    Don't forget to add collider and check isTrigger, when the player collide the object, he will jump to the mentioned scene.
     
    braindit80 and abrahamdeccy like this.
  12. RochelleShirley

    RochelleShirley

    Joined:
    Jan 12, 2023
    Posts:
    1
    Do I put this on the object in question or the Player, @iSoloXIV?