Search Unity

to jump to specific scene and not next level...

Discussion in 'Scripting' started by Patrk, Jun 18, 2018.

  1. Patrk

    Patrk

    Joined:
    Apr 9, 2017
    Posts:
    48
    Hi,

    I have a plane area in Unity 2017.1.1f1 that is a capsule collider and is tagged as 'Player'.
    I want to use it as a trigger to jump to various different scenes/levels when I throw x5 seperate objects on it individually, so it jumps to the relevant scene'level. i.e object 1 when thrown, jumps to scene 1, object 2 jumps to scene 2 etc......

    the objects have this scrip below attached...

    Code (csharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5. using UnityEngine.SceneManagement;
    6.  
    7. public class GoToLevel : MonoBehaviour {
    8.  
    9.      public string nextLevel;
    10.  
    11.      private void OnTriggerEnter(Collider col)
    12.      {
    13.           if (col.CompareTag ("Player"))
    14.               {
    15.                SceneManager.LoadScene (nextLevel);
    16.               }
    17.          }
    18. }
    19.  
    The problem is every object (object 1, 2 3, 4 or 5) when thrown just jumps to the next level i.e.scene 2.
    The component fields on the object say 'Next Level - Level2' but when I change that to 'level3' it breaks as the #C code says NextLevel and not level1, level 2 etc..what do I have to put in each script for each object to make it jump to say Levell 3 and not simply NextLevel?

    thanks
     
  2. johne5

    johne5

    Joined:
    Dec 4, 2011
    Posts:
    1,133
    I think you need two things.
    First verify or change the scene name to level1, level2, level3.... verify that these scenes are in the build menu. you will get an error if they are not.
    second, update the code

    Code (CSharp):
    1. public class GoToLevel : MonoBehaviour
    2. {
    3.  
    4.     public int nextLevel;  /change to an int
    5.  
    6.     private void OnTriggerEnter(Collider col)
    7.     {
    8.         if (col.CompareTag ("Player"))
    9.         {
    10.             SceneManager.LoadScene ("level" + nextLevel); //load the level with a string that the end number changes
    11.         }
    12.     }
    13. }
     
  3. Patrk

    Patrk

    Joined:
    Apr 9, 2017
    Posts:
    48
    Hi Johne,

    Many thanks, have got it working, I had the scenes named Level1 etc but had forgotten to add the extra new scenes to the build and also when changed the code to intergers it all worked. thanks again P