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

Question Level Menu Not Working..

Discussion in 'Scripting' started by SmallLion, Mar 8, 2021.

  1. SmallLion

    SmallLion

    Joined:
    Oct 7, 2020
    Posts:
    20
    HI there,My Level Menu not working.I made it to unlock one by one(using interactable),but that's not working correctly.When I passed level 02,Level 05 button get unlocked.What I'm doing wrong?Here is my Game Manager,
    Code (CSharp):
    1. public class GameManager : MonoBehaviour
    2. {
    3.     bool gamehasEnded = true;
    4.  
    5.     public Text timeText;
    6.     public GameObject Timercanva;
    7.    
    8.     public GameObject MainText;
    9.     public int LevelToUnlock = 2;
    10.     public GameObject WinUi;
    11.     public GameObject WrongUi;
    12.     int sceneIndex, LevelPassed;
    13.  
    14.     void Start()
    15.     {
    16.         sceneIndex = SceneManager.GetActiveScene().buildIndex;
    17.         LevelPassed = PlayerPrefs.GetInt("LevelPassed");
    18.     }
    19.  
    20.     public void QAComplete()
    21.     {
    22.         WinUi.SetActive(true);
    23.         Timercanva.SetActive(false);
    24.         MainText.SetActive(false);
    25.         timeText.enabled = false;
    26.         if (sceneIndex == 9)
    27.         {
    28.             SceneManager.LoadScene(2);
    29.         }
    30.         else
    31.          if (LevelPassed > sceneIndex)
    32.         {
    33.             PlayerPrefs.SetInt("LevelPassed", sceneIndex);
    34.         }
    35.  
    36.        
    37.      
    38.  
    39.    
    40.  
    41.     }
    42.        public void GameOver()
    43.         {
    44.  
    45.             if (gamehasEnded == false)
    46.             {
    47.                 gamehasEnded = true;
    48.                 WrongUi.SetActive(true);
    49.                 Timercanva.SetActive(false);
    50.                
    51.                 timeText.enabled = false;
    52.  
    53.             }
    54.  
    55.         }
    56.  
    57.        }  
    Level Menu,
    Code (CSharp):
    1.  public Button Level2, Level3, Level4, Level5, Level6;
    2.     int LevelPassed;
    3.  
    4.     void Start()
    5.     {
    6.         LevelPassed = PlayerPrefs.GetInt("LevelPassed");
    7.      
    8.         Level2.interactable = false;
    9.         Level3.interactable = false;
    10.         Level4.interactable = false;
    11.         Level5.interactable = false;
    12.         Level6.interactable = false;
    13.        
    14.  
    15.          switch (LevelPassed)
    16.         {
    17.             case 1:
    18.                 Level2.interactable = true;
    19.                 break;
    20.  
    21.             case 2:
    22.                 Level3.interactable = true;
    23.                 break;
    24.             case 3:
    25.                 Level4.interactable = true;
    26.                 break;
    27.             case 4:
    28.                 Level5.interactable = true;
    29.                 break;
    30.             case 5:
    31.                 Level6.interactable = true;
    32.                 break;
    33.         }
    34.        
    35.      
    36.     }
    37.  
    38.     public void levelToLoad(int Level)
    39.     {
    40.         SceneManager.LoadScene(Level);
    41.     }
    42.  
    43. }
    44.  
    Build Setting - OK
    Buttons inserted correctly.

    Please help!(sorry for bad english)
     
  2. FlashMuller

    FlashMuller

    Joined:
    Sep 25, 2013
    Posts:
    449
    Are you sure the plain buildIndex of the scene is correct? If you have a logo and a main menu and whatever else the buildindex is not the level number. The code looks ok.
     
  3. SmallLion

    SmallLion

    Joined:
    Oct 7, 2020
    Posts:
    20
    Yes it is correct.But not works, If I won 2nd level, button of 5th level get unlocked..........:(
     
  4. Nakel

    Nakel

    Joined:
    Sep 28, 2017
    Posts:
    106
    Make sure the level index of each scene you are using is right and in the correct order.
     
  5. SmallLion

    SmallLion

    Joined:
    Oct 7, 2020
    Posts:
    20
    I checked them all.I think somewhat wrong with scripts(PlayerPrefs)
     
  6. FlashMuller

    FlashMuller

    Joined:
    Sep 25, 2013
    Posts:
    449
    Time to step through your code then. Set breakpoints and check the numbers saved and loaded and if they are what you expect.
     
  7. SmallLion

    SmallLion

    Joined:
    Oct 7, 2020
    Posts:
    20
    I will.Edit > Clear All PlayerPrefs will also work..
     
    Nakel likes this.