Search Unity

Unable to disable buttons in canvas

Discussion in 'Scripting' started by DoomDude99, May 23, 2019.

  1. DoomDude99

    DoomDude99

    Joined:
    May 11, 2019
    Posts:
    87
    This question is a follow up of this one:

    https://forum.unity.com/threads/creating-a-gameobject-array-from-buttons.682195/#post-4565677

    I have a script "CanvasScript.cs":

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.SceneManagement;
    5.  
    6. public class CanvasScript : MonoBehaviour
    7. {
    8.     public List<GameObject> startButtonObjects;
    9.     public List<GameObject> ingameButtonObject;
    10.  
    11.     void Start() {
    12.         foreach (GameObject startObject in startButtonObjects)
    13.         {
    14.             startObject.gameObject.SetActive(true);
    15.         }
    16.  
    17.         //Activate all AfterStartObjects
    18.         foreach (GameObject afterStartObject in ingameButtonObject)
    19.         {
    20.             afterStartObject.gameObject.SetActive(false);
    21.         }
    22.     }
    23.  
    24.     public void StartButtonPressed(string parameterThatIsnotUsed)
    25.     {
    26.         Debug.Log("Start button pressed");
    27.  
    28.         foreach (GameObject startObject in startButtonObjects)
    29.         {
    30.             startObject.gameObject.SetActive(false);
    31.         }
    32.  
    33.         //Activate all AfterStartObjects
    34.         foreach (GameObject afterStartObject in ingameButtonObject)
    35.         {
    36.             afterStartObject.gameObject.SetActive(true);
    37.         }
    38.     }
    39.  
    40.     public void QuitButtonPressed(string parameterThatIsnotUsed)
    41.     {
    42.  
    43.     }
    44.  
    45.     public void ChangeScene(string parameterThatIsnotUsed)
    46.     {
    47.  
    48.     }
    49. }
    50.  
    that should disable/enable buttons depending on user interaction, except that it doesn't.



    How can I enable/disable buttons within a C# script?
     
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    In your screenshot, your two arrays on CanvasScript are empty. That would certainly make them not activate deactivate when this script is triggered.
     
    DoomDude99 likes this.
  3. DoomDude99

    DoomDude99

    Joined:
    May 11, 2019
    Posts:
    87
    Whoops :)))
     
  4. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,836
    When asking for help, you should say what actual behavior you observed and how it differs from the desired result.

    Based on this vague description, there are a lot of things that could be wrong, but one thing I notice is that the lists "startButtonObjects" and "ingameButtonObject" are both empty in your screenshot. That seems likely to be an error.

    More generally on your topic question, there's three different kinds of "disabling" you might commonly do to Unity buttons:
    1. You can use GameObject.SetActive(false), which makes the whole thing disappear from your game
    2. You can use Behavior.enabled = false, which prevents the button from being updated.
    3. You can use Button.interactable = false, which causes the button to use its disabled appearance (by default, it turns gray) and become unclickable
     
    DoomDude99 likes this.