Search Unity

Problems with deactivating buttons

Discussion in 'Scripting' started by bigdog16, Jun 24, 2020.

  1. bigdog16

    bigdog16

    Joined:
    Mar 5, 2018
    Posts:
    12
    Hello everybody,

    my problem is that when i change from the menu to the level selection scene after login everything works normally. Say the buttons that should be deactivated are also. As soon as I change to a level and go back to the level selection scene, all buttons are activated, although the script works normally.

    sorry for my english.

    Here my code;
    Code (CSharp):
    1. public class LevelSelector : MonoBehaviour
    2. {
    3.     public Button[] levelButtons;
    4.  
    5.  
    6.     public static LevelSelector LVL;
    7.     public int levelReached;
    8.  
    9.  
    10.  
    11.     private void OnEnable()
    12.     {
    13.  
    14.  
    15.  
    16.         if (LVL == null)
    17.         {
    18.             LVL = this;
    19.         }
    20.     }
    21.     void Start()
    22.     {
    23.  
    24.      
    25.  
    26.         GetLevelAgypt();
    27.  
    28.    
    29.  
    30.  
    31.  
    32.     }
    33.     public void Play(string sceneName)
    34.     {
    35.  
    36.         SceneManager.LoadScene(sceneName);
    37.         Time.timeScale = 1;
    38.  
    39.     }
    40.  
    41.     public void LevelDataToString(string Level)
    42.     {
    43.             levelReached = int.Parse(Level);
    44.  
    45.             for (int j = 0; j < levelButtons.Length; j++)
    46.             {
    47.  
    48.  
    49.                 if (j +1 > int.Parse(Level) )
    50.  
    51.                 {
    52.                     levelButtons[j].interactable = false;
    53.                 levelButtons[j].GetComponent<Image>().color = new Color32(255, 255, 255, 148);
    54.                 Debug.Log(levelButtons[j]);
    55.                 }
    56.  
    57.  
    58.             }
    59.        
    60.     }
    61.  
    62.     public void GetLevelAgypt()
    63.     {
    64.  
    65.         PlayFabClientAPI.GetUserData(new GetUserDataRequest()
    66.         {
    67.             PlayFabId = PlayFabLogin.myID,
    68.             Keys = null
    69.         }, result =>
    70.         {
    71.             Debug.Log("Got user data:");
    72.             if (result.Data == null || !result.Data.ContainsKey("LevelAgypt")) Debug.Log("Level not set");
    73.             else LevelSelector.LVL.LevelDataToString(result.Data["LevelAgypt"].Value);
    74.         }, (error) =>
    75.         {
    76.             Debug.Log("Got error retrieving user data:");
    77.             Debug.Log(error.GenerateErrorReport());
    78.         });
    79.     }
    80. }
     

    Attached Files:

  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,697
    Traditionally you would set the desired button states in your Start() method.
     
  3. bigdog16

    bigdog16

    Joined:
    Mar 5, 2018
    Posts:
    12
    OK thank you:oops: