Disabling UI after setting the alpha to 0.

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:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Threading;
using System.Threading.Tasks;

public class FadeScript : MonoBehaviour
{
    [SerializeField] private CanvasGroup myUICanvas;
    [SerializeField] private bool fadeIn = false;
    [SerializeField] private bool fadeOut = false;

    public void ShowUI()
    {
        fadeIn = true;
    }// end of show ui
    public void hideUI()
    {
       
        fadeOut = true;
       
       
       
    }// end of hide ui


    // Update is called once per frame
    void Update()
    {
        if (fadeIn)
        {
            if(myUICanvas.alpha < 1)
            {
                myUICanvas.alpha += Time.deltaTime;
                if(myUICanvas.alpha >= 1)
                {
                    fadeIn = false;
                }// end of if myUIcanvas.alpha >= 1
            }// end of if myUIcanvas.alpha >= 1
        }// end of if fade IN

        if (fadeOut)
        {
            if (myUICanvas.alpha >= 0)
            {
                myUICanvas.alpha -= Time.deltaTime;
               
                if (myUICanvas.alpha == 0)
                {
                    fadeOut = false;
                }// end of if myUIcanvas.alpha >= 1
               
            }// end of if myUIcanvas

            if (myUICanvas.alpha == 0)
            {
                myUICanvas.interactable = false;
            }
        }// end of fadeOUT
        }// end of update
}

Hi maiza989, here are some example on how to disable the UI. Hope it help

You shouldn’t need 30 lines of code to fade something… 2 variables and 2 lines should be plenty.

Fading, simple and easy: