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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

buttons aren't enabling now

Discussion in 'Scripting' started by isanvel, Dec 18, 2019.

  1. isanvel

    isanvel

    Joined:
    Mar 21, 2019
    Posts:
    41
    so i have my script running my switch with it's value 3 but my buttons aren't enabling. i disabled them on editor so when i started my game they would enable but they don't enable at all

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class LevelController : MonoBehaviour
    7. {      
    8.     string[] levelName = new string[15];
    9.    // SceneLoader sceneLoader;
    10.     FinishLevel finishLevel;
    11.     [SerializeField] Image[] locks;
    12.     [SerializeField] Button[] locksBtn;
    13.     int myLevelCompleted;
    14.  
    15.     private void Awake()
    16.     {
    17.         finishLevel = GetComponent<FinishLevel>();
    18.         GetLevelPlayerPref();
    19.         var myLevelCompleted = PlayerPrefs.GetInt("levelComplete");
    20.     }
    21.  
    22.     // Start is called before the first frame update
    23.     void Start()
    24.     {      
    25.        /* finishLevel = GetComponent<FinishLevel>();      
    26.         GetLevelPlayerPref();
    27.         UnlockLevel(); */
    28.     }
    29.  
    30.     // Update is called once per frame
    31.     void Update()
    32.     {
    33.         UnlockLevel();
    34.     }
    35.  
    36.     void GetLevelPlayerPref()
    37.     {
    38.       myLevelCompleted = PlayerPrefs.GetInt("levelComplete"); // gets the last saved playerpref key
    39.         Debug.Log(myLevelCompleted);
    40.             PlayerPrefs.HasKey("levelComplete");
    41.     }
    42.  
    43.     void UnlockLevel()
    44.     {
    45.         Debug.Log("Number is " + myLevelCompleted);
    46.         switch (myLevelCompleted)
    47.         {          
    48.             case 2:
    49.                 Debug.Log("test");
    50.                 locks[0].enabled = false;
    51.                     locksBtn[0].enabled = true;              
    52.                 break;          
    53.             case 3:
    54.                
    55.                 locks[1].enabled = false;
    56.                     locksBtn[1].enabled = true;      
    57.                 break;
    58.             case 4:
    59.                     locks[2].enabled = false;
    60.                     locksBtn[2].enabled = true;
    61.                 break;
    62.             case 5:
    63.                     locks[3].enabled = false;
    64.                     locksBtn[3].enabled = true;
    65.                 break;
    66.             case 6:
    67.                     locks[4].enabled = false;
    68.                     locksBtn[4].enabled = true;
    69.                 break;
    70.             case 7:
    71.                     locks[5].enabled = false;
    72.                     locksBtn[5].enabled = true;
    73.                 break;
    74.             case 8:
    75.                     locks[6].enabled = false;
    76.                     locksBtn[6].enabled = true;
    77.                 break;
    78.         }
    79.     }
    80. }
    81.  
     
  2. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    In Awake() you are creating a new local variable named myLevelCompleted, assigning it the value from PlayerPrefs, then never doing anything with the variable. Not sure why that is there. I also don't know what you are doing with HasKey on line 40. It returns a bool, but you're not doing anything with it.

    What does the Debug.Log line on line 45 say in the console?
    Do you have any console errors?
    Are you sure you have filled both the locks and locksBtn arrays in the inspector?
     
  3. isanvel

    isanvel

    Joined:
    Mar 21, 2019
    Posts:
    41
    i declared the myLevelCompleted variable so i could use it in the switch, oterwise i can't use it
    also the haskey i forgot to comment it. buttons are all in the inspector but they aren't enabling
     
  4. Carwashh

    Carwashh

    Joined:
    Jul 28, 2012
    Posts:
    746
    Remove ‘var’ from line 19
     
  5. isanvel

    isanvel

    Joined:
    Mar 21, 2019
    Posts:
    41
    did it, still didn't work
     
  6. Carwashh

    Carwashh

    Joined:
    Jul 28, 2012
    Posts:
    746
    You didn’t answer Joes question.
    Line 45, what does that print out? Your ‘myLevelCompleted’ value could be <2 and nothing will happen.
     
    Joe-Censored likes this.
  7. isanvel

    isanvel

    Joined:
    Mar 21, 2019
    Posts:
    41
    it prints 3, the lock image disappears but the button won't enable for some reason
     
  8. Carwashh

    Carwashh

    Joined:
    Jul 28, 2012
    Posts:
    746
    Your array is of buttons. Is the game object disabled?
     
  9. isanvel

    isanvel

    Joined:
    Mar 21, 2019
    Posts:
    41
    yea they are
     
  10. Carwashh

    Carwashh

    Joined:
    Jul 28, 2012
    Posts:
    746
    Then you’re never enabling the game object, you just turn the button component on.
     
    Joe-Censored likes this.
  11. isanvel

    isanvel

    Joined:
    Mar 21, 2019
    Posts:
    41
    look, they point to buttons
    upload_2019-12-18_19-0-51.png
     
  12. Carwashh

    Carwashh

    Joined:
    Jul 28, 2012
    Posts:
    746
    Yes, they point to buttons (these are components ON a GameObject), and the GameObjects are disabled. All you are doing is changing the active state of the component.

    Unless you actually need to access the button component, change that array to GameObject[].
     
    Joe-Censored likes this.
  13. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    You can also enable the button's GameObject through the component reference
    Code (csharp):
    1. locksBtn[4].gameObject.SetActive(true);
    For a button to actually display, the component itself has to be enabled, the GameObject it is attached to must be active, and every parent GameObject must also be active. If any one of those things is disabled or inactive then you won't see the button. That's just the basics of how the hierarchy, GameObjects, and components work in Unity, so make sure your design accounts for that.
     
    Carwashh likes this.