Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Unity Doesnt Load Next Levels

Discussion in 'General Graphics' started by iN-Liquid, Feb 13, 2015.

  1. iN-Liquid

    iN-Liquid

    Joined:
    Feb 11, 2015
    Posts:
    6
    I have set up my game to where a player goes into a cube and when they reach the cube, they enter the next level/scene in the build settings, but no matter how i put the code, it doesn't work the way I want it to.

    Code (CSharp):
    1. public class GameManager : MonoBehaviour {
    2.     public static int currentScore;
    3.     public static int highScore;
    4.     public static int currentLevel = 0;
    5.     public static int unlockedLevel;
    6.  
    7.  
    8.  
    9.  
    10.  
    11.     public static void CompleteLevel ()
    12.     {
    13.  
    14.         Application.LoadLevel(currentLevel + 1);
    15.  
    16.     }
    17. }
    ^This Code takes me from level 1 to level 2, but once it gets to level 2, it stops proceeding.^

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class GameManager : MonoBehaviour {
    5.     public static int currentScore;
    6.     public static int highScore;
    7.     public static int currentLevel = 0;
    8.     public static int unlockedLevel;
    9.  
    10.  
    11.  
    12.  
    13.  
    14.     public static void CompleteLevel ()
    15.     {
    16.  
    17.         Application.LoadLevel(currentLevel += 1);
    18.  
    19.     }
    20. }
    21.  
    ^This code will take me to level 1, then two, then two again, then four.^

    The Difference between the two codes is "Application.LoadLevel += 1"

    I was wondering if somebody could help me make it so it goes level 1, 2, 3, 4, 5, 6 and so on.
    Thanks!
     
  2. SteveJ

    SteveJ

    Joined:
    Mar 26, 2010
    Posts:
    3,066
    You'll need to post more complete code. Where are you calling the CompleteLevel() function from? Is this GameManager class a singleton?
     
  3. iN-Liquid

    iN-Liquid

    Joined:
    Feb 11, 2015
    Posts:
    6
    Just realized I posted this in Graphics. Oops.

    I'm a beginner so I don't know all the slang like singleton lol.

    But the CompleteLevel() is called in the playerMovement script when the player arrives at the square tagged "Goal"
    Lines 40-46

    Code (CSharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class PlayerMovement : MonoBehaviour {
    6.     public float moveSpeed;
    7.     private float maxSpeed = 5f;
    8.     private Vector3 input;
    9.     private Vector3 spawn;
    10.     public GameObject deathParticle;
    11.     public float deathHeight;
    12.     // Use this for initialization
    13.     void Start () {
    14.         spawn = transform.position;
    15.     }
    16.  
    17.     // Update is called once per frame
    18.     void Update ()
    19.     {
    20.         input = new Vector3 (Input.GetAxisRaw ("Horizontal"), 0, Input.GetAxisRaw ("Vertical"));
    21.  
    22.         if (rigidbody.velocity.magnitude < maxSpeed)
    23.         {
    24.             rigidbody.AddForce (input * moveSpeed);
    25.         }
    26.         if(transform.position.y < deathHeight)
    27.         {
    28.             Die ();
    29.         }
    30.     }
    31.  
    32.  
    33.     void OnCollisionEnter(Collision otherCollision)
    34.         {
    35.         if( otherCollision.gameObject.tag == "Enemy" )
    36.         {
    37.             Die();
    38.         }
    39.     }
    40.         void OnTriggerEnter(Collider other)
    41.     {
    42.         if (other.transform.tag == "Goal") {
    43.             GameManager.CompleteLevel();
    44.        
    45.         }
    46.         }
    47.  
    48.     void Die()
    49.     {          
    50.         Instantiate(deathParticle, transform.position, Quaternion.identity);
    51.             transform.position = spawn;
    52.     }
    53.         }
    54.  
    55.    
    56.        
     
  4. orb

    orb

    Joined:
    Nov 24, 2010
    Posts:
    3,033
  5. theANMATOR2b

    theANMATOR2b

    Joined:
    Jul 12, 2014
    Posts:
    7,790
    Now that's funny right there!
     
  6. bigSadFace

    bigSadFace

    Joined:
    Aug 18, 2014
    Posts:
    116
    It would be better to have your gamemanager a singleton; however as a quick fix, assuming you are using sequential level numbers:

    Replace:

    Application.LoadLevel(currentLevel +1);

    With:

    Application.LoadLevel (Application.loadedLevel + 1)