buttons aren't enabling now

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

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class LevelController : MonoBehaviour
{      
    string[] levelName = new string[15];
   // SceneLoader sceneLoader;
    FinishLevel finishLevel;
    [SerializeField] Image[] locks;
    [SerializeField] Button[] locksBtn;
    int myLevelCompleted;

    private void Awake()
    {
        finishLevel = GetComponent<FinishLevel>();
        GetLevelPlayerPref();
        var myLevelCompleted = PlayerPrefs.GetInt("levelComplete");
    }

    // Start is called before the first frame update
    void Start()
    {       
       /* finishLevel = GetComponent<FinishLevel>();       
        GetLevelPlayerPref();
        UnlockLevel(); */
    }

    // Update is called once per frame
    void Update()
    {
        UnlockLevel();
    }

    void GetLevelPlayerPref()
    {
      myLevelCompleted = PlayerPrefs.GetInt("levelComplete"); // gets the last saved playerpref key
        Debug.Log(myLevelCompleted);
            PlayerPrefs.HasKey("levelComplete");
    }

    void UnlockLevel()
    {
        Debug.Log("Number is " + myLevelCompleted);
        switch (myLevelCompleted)
        {           
            case 2:
                Debug.Log("test");
                locks[0].enabled = false;
                    locksBtn[0].enabled = true;              
                break;           
            case 3:
               
                locks[1].enabled = false;
                    locksBtn[1].enabled = true;      
                break;
            case 4:
                    locks[2].enabled = false;
                    locksBtn[2].enabled = true;
                break;
            case 5:
                    locks[3].enabled = false;
                    locksBtn[3].enabled = true;
                break;
            case 6:
                    locks[4].enabled = false;
                    locksBtn[4].enabled = true;
                break;
            case 7:
                    locks[5].enabled = false;
                    locksBtn[5].enabled = true;
                break;
            case 8:
                    locks[6].enabled = false;
                    locksBtn[6].enabled = true;
                break;
        }
    }
}

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?

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

Remove ‘var’ from line 19

did it, still didn’t work

You didn’t answer Joes question.
Line 45, what does that print out? Your ‘myLevelCompleted’ value could be <2 and nothing will happen.

1 Like

it prints 3, the lock image disappears but the button won’t enable for some reason

Your array is of buttons. Is the game object disabled?

yea they are

Then you’re never enabling the game object, you just turn the button component on.

1 Like

look, they point to buttons
5296131--531696--upload_2019-12-18_19-0-51.jpg

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[ ].

1 Like

You can also enable the button’s GameObject through the component reference

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.

1 Like