Search Unity

GameManager issue on level load

Discussion in 'Scripting' started by meshari94, Nov 23, 2014.

  1. meshari94

    meshari94

    Joined:
    Nov 22, 2014
    Posts:
    13
    Hello everyone,

    So basically i am having a couple of issues with the levels of the game. So here is what's happening:

    I have a main menu of the game, it has the play, levels, and quit buttons.

    Play: takes me to the first level of the scene.

    Levels: takes me to the "Levels select" scene.

    But here is the big problem. So when i start the first level from the "Play", it begins level 1 and goes on to the other levels when 1 is completed.

    But when i select a level from the "Levels Select" scene, let's say level 5, and then i finish level five, it doesn't take me to level 6 as it should, but rather takes me to level 2. I know the problem is from the GameManager, but i can't seem to know the right function to fix this.

    This is the GameManager:

    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.  
    8.     public static int currentLevel = 0;
    9.  
    10.  
    11.     public float startTime;
    12.     private string currentTime;
    13.  
    14.     void Start()
    15.     {
    16.         DontDestroyOnLoad(gameObject);
    17.     }
    18.  
    19.     public static void CompleteLevel()
    20.     {
    21.         if (currentLevel < 7)
    22.         {
    23.             currentLevel += 1;
    24.             Application.LoadLevel(currentLevel);              
    25.         } else {
    26.             print ("Well done for completing the game, more levels coming soon ;)");
    27.         }  
    28.     }
    29.  
    30.  
    31. }
    And this is the LevelsSelect Scene:

    Code (CSharp):
    1. var isLevelone=false;
    2. var isLeveltwo=false;
    3. var isLevelthree=false;
    4. var isLevelfour=false;
    5. var isLevelfive=false;
    6. var isLevelsix=false;
    7. var isLevelseven=false;
    8. var isBack=false;
    9.  
    10. function OnMouseEnter(){
    11.     //change text color
    12.     renderer.material.color=Color.red;
    13. }
    14.  
    15. function OnMouseExit(){
    16.     //change text color
    17.     renderer.material.color=Color.white;
    18. }
    19.  
    20. function OnMouseUp(){
    21.     //is this quit
    22.     if (isLevelone==true) {
    23.         Application.LoadLevel(0);
    24.     }
    25.    
    26.     else if (isLeveltwo==true) {
    27.         Application.LoadLevel(1);
    28.     }
    29.    
    30.     else if (isLevelthree==true) {
    31.         Application.LoadLevel(2);
    32.     }
    33.    
    34.     else if (isLevelfour==true) {
    35.         Application.LoadLevel(3);
    36.     }
    37.    
    38.     else if (isLevelfive==true) {
    39.         Application.LoadLevel(4);
    40.     }
    41.    
    42.     else if (isLevelsix==true) {
    43.         Application.LoadLevel(5);
    44.     }
    45.    
    46.     else if(isLevelseven==true){
    47.         Application.LoadLevel(6);
    48.        
    49.     }
    50.    
    51.     else if(isBack==true) {
    52.         Application.LoadLevel(7);
    53.     }
    54. }
    55.  
    56. function Update(){
    57.     //quit game if escape key is pressed
    58.     if (Input.GetKey(KeyCode.Escape)){
    59.              Application.Quit();
    60.     }
    61. }
     
  2. Fraconte

    Fraconte

    Joined:
    Dec 6, 2013
    Posts:
    327
    Everytime you call ApplicationLoadLevel() you have to also update GameManager.currentLevel.
    I think you could also get rid of all that boolean and if(isLevelxxx==true) just by setting currentLevel and calling Application.LoadLevel(GameManager.currentLevel).
     
  3. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,533
    I set up my GameManare class to have a function to load the level, and it calls Application.LoadLevel. This way it know when a level changes and it sets the current level, because it did the loading of it.
     
    Fraconte likes this.
  4. meshari94

    meshari94

    Joined:
    Nov 22, 2014
    Posts:
    13
    Yes i do understand, but the problem is that i have different scenes, and the level select scene has its own script, while the GameManager has it's own script, so how would Application.LoadLevel work if i have both scripts setup differently?

    Thanks for the reply.
     
  5. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,533
    You pass in to the method what's need for that specific scene.