Search Unity

Question how do I disable a canvas with C#?

Discussion in 'UGUI & TextMesh Pro' started by deryldev, Dec 21, 2022.

  1. deryldev

    deryldev

    Joined:
    Nov 25, 2022
    Posts:
    6
    Code (CSharp):
    1. public class LoginManage : MonoBehaviour
    2. {
    3.     public GameObject one;
    4.     public Text two;
    5.     public Text tree;
    6.     public GameObject four;
    7.     public static string text1;
    8.     public static string text2;
    9.     // Start is called before the first frame update
    10.     void Start()
    11.     {
    12.        
    13.     }
    14.  
    15.     // Update is called once per frame
    16.     void Update()
    17.     {
    18.        
    19.     }
    20.  
    21.     public void onClick()
    22.     {
    23.         two.text = text1;
    24.         tree.text = text2;
    25.         if(text1=="admin"){
    26.             if(text2=="admin")
    27.             {
    28.                 one.SetActive(false);
    29.             }
    30.             else{
    31.                 four.SetActive(true);
    32.             }
    33.         }
    34.         else{
    35.             four.SetActive(true);
    36.         }
    37.     }
    38. }
    39.  
    variable one is canvas, two is text username, tree is text password, and four is text if password is wrong.
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,742
    You can either set the .gameObject of the Canvas to inactive, or simply toggle the .enabled field on the Canvas.

    What part are you struggling with?

    How to report your problem productively in the Unity3D forums:

    http://plbm.com/?p=220

    This is the bare minimum of information to report:

    - what you want
    - what you tried
    - what you expected to happen
    - what actually happened, especially any errors you see
    - links to documentation you used to cross-check your work (CRITICAL!!!)

    You may edit your post above.
     
  3. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,492
    You should ask on the dedicated UI forums as 2D is not UI. I'll move your post.