I want to make text appear for a few seconds and then disappear.

The text is when player don’t have the item. It’s like “you don’t have this item…”. It’s on line 39 & 40. Basically i want to make it from true to false for noKeyUI and false to true for interactUI. This is my code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class PlayerInteraction : MonoBehaviour
{
    [SerializeField] private Text interactUI;
    [SerializeField] private Text noKeyUI;
    public GameObject currentObj = null;
    public ObjectInteraction currentObjScript = null;
    public Inventory inventory;
    
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.E) && currentObj)
        {
            if (currentObjScript.inventory)
            {
                inventory.AddItem(currentObj);
            }

            //check if the door can be opened
            if (currentObjScript.canOpen)
            {
                //check if the door is locked
                if (currentObjScript.locked)
                {
                    //check if player have the key - unlock if have the key
                    if (inventory.FindItem(currentObjScript.itemNeed))
                    {
                        currentObjScript.locked = false;                       
                    }

                    else
                    {
                        noKeyUI.gameObject.SetActive(true);
                        interactUI.gameObject.SetActive(false);
                    }
                }

                else
                {
                    currentObjScript.SwitchLevel();
                }
            }
        }
    }

    void OnTriggerEnter2D(Collider2D other)
    {
        if (other.CompareTag("InterObj"))
        {
            interactUI.gameObject.SetActive(true);
            currentObj = other.gameObject;
            currentObjScript = currentObj.GetComponent<ObjectInteraction>();
        }
    }

    void OnTriggerExit2D(Collider2D other)
    {
        interactUI.gameObject.SetActive(false);

        if (other.CompareTag("InterObj"))
        {
            if (other.gameObject == currentObj)
            {
                currentObj = null;
            }
        }
    }
}

May use Invoke();

I’d recommend using a coroutine. Coroutines are very useful for timers, and are quite easy to use once you know how. You can do:

IEnumerator CoroutineName() { yield return new WaitForSeconds(delay); noKeyUI.active = false; }
You can call it whenever you want by doing StartCoroutine(CoroutineName());.
Hope this helps!