Search Unity

  1. Get all the Unite Berlin 2018 news on the blog.
    Dismiss Notice
  2. Unity 2018.2 has arrived! Read about it here.
    Dismiss Notice
  3. Improve your Unity skills with a certified instructor in a private, interactive classroom. Learn more.
    Dismiss Notice
  4. ARCore is out of developer preview! Read about it here.
    Dismiss Notice
  5. Magic Leap’s Lumin SDK Technical Preview for Unity lets you get started creating content for Magic Leap One™. Find more information on our blog!
    Dismiss Notice
  6. Want to see the most recent patch releases? Take a peek at the patch release page.
    Dismiss Notice

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
    gingerails and theANMATOR2b like this.
  2. BryanO

    BryanO

    Joined:
    Jan 8, 2014
    Posts:
    128
    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:
    24
    Add a destroy game object
     
  4. Mrawesome109

    Mrawesome109

    Joined:
    Jan 7, 2018
    Posts:
    1
    :)thank you