Dictionary Value to Disable Options

Scene Button Disabled based on Dictionary Value

In the code below, located in a State file associated with a Scene, there's logic that checks the GameData Dictionary to see if the key-value pair: "choice1": "heart" exists in the dictionary. If it exists, then ButtonOption1 is disabled. This logic is executed as soon as the Corresponding scene is loaded, so the button is disabled before the Scene is displayed. This is a toy example, but this is a reasonable place for this logic to be located. It illustrates the general idea of checking dictionary values to determine options available as a scene is loaded. A Dictionary< string, string > provides flexibility to include any user choices that can be used to constrain the user's options.

Scene2State.cs Logic to Disable Button depending on Dictionary values

 public void InitializeObjectRefs()
    {
        optionBtn1 = GameObject.Find("ButtonOption1").GetComponent<Button>();
        optionBtn1.onClick.AddListener(LoadScene3);

        string choiceValue = GameData.instanceRef.GetChoice("choice1");
        if(choiceValue == "heart")
        {
            GameObject btn1 = optionBtn1.gameObject;
            btn1.SetActive(false);
        }
        optionBtn2 = GameObject.Find("ButtonOption2").GetComponent<Button>();
        optionBtn2.onClick.AddListener(LoadMiniGame);

        Debug.Log("Initialize Refs - Scene2State");
    }

Selecting the heart option eliminates the option to return to the BeginScene from Scene 2. Logic could be added to disable the Choice panel once the heart button has been selected, otherwise, the dictionary updates the "choice1" value each time the player selects a button.

Last updated