Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Question Disabling UI after setting the alpha to 0.

Discussion in 'UGUI & TextMesh Pro' started by maiza989, Jul 18, 2022.

  1. maiza989

    maiza989

    Joined:
    Jun 11, 2022
    Posts:
    2
    I have a question. I have a fade out script when when you click a button it fade the UI canvas group by setting the alpha to 0. I added a trigger event to it for hover over sound. now when I click the button to fade the UI and re-hover over the same place the trigger event triggers and play the sound. I tried adding a if statement to check if the alpha is 0 if so make the UI un intractable. but I might not have dont it correctly. here is my code so far:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using System.Threading;
    5. using System.Threading.Tasks;
    6.  
    7. public class FadeScript : MonoBehaviour
    8. {
    9.     [SerializeField] private CanvasGroup myUICanvas;
    10.     [SerializeField] private bool fadeIn = false;
    11.     [SerializeField] private bool fadeOut = false;
    12.  
    13.     public void ShowUI()
    14.     {
    15.         fadeIn = true;
    16.     }// end of show ui
    17.     public void hideUI()
    18.     {
    19.        
    20.         fadeOut = true;
    21.        
    22.        
    23.        
    24.     }// end of hide ui
    25.  
    26.  
    27.     // Update is called once per frame
    28.     void Update()
    29.     {
    30.         if (fadeIn)
    31.         {
    32.             if(myUICanvas.alpha < 1)
    33.             {
    34.                 myUICanvas.alpha += Time.deltaTime;
    35.                 if(myUICanvas.alpha >= 1)
    36.                 {
    37.                     fadeIn = false;
    38.                 }// end of if myUIcanvas.alpha >= 1
    39.             }// end of if myUIcanvas.alpha >= 1
    40.         }// end of if fade IN
    41.  
    42.         if (fadeOut)
    43.         {
    44.             if (myUICanvas.alpha >= 0)
    45.             {
    46.                 myUICanvas.alpha -= Time.deltaTime;
    47.                
    48.                 if (myUICanvas.alpha == 0)
    49.                 {
    50.                     fadeOut = false;
    51.                 }// end of if myUIcanvas.alpha >= 1
    52.                
    53.             }// end of if myUIcanvas
    54.  
    55.             if (myUICanvas.alpha == 0)
    56.             {
    57.                 myUICanvas.interactable = false;
    58.             }
    59.         }// end of fadeOUT
    60.         }// end of update
    61. }
    62.  
     
  2. MaxwellTan

    MaxwellTan

    Unity Technologies

    Joined:
    Mar 3, 2022
    Posts:
    75
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    39,020