UI Text

Hey, I made 2 scripts for text, means everytime I press on a key the text appear for 3 seconds and fade out. I made one script for the text and one for the fade. so let say i press #1, a message appear.
It wroks great only for one time and only for one key, means if i want 2 texts, only one of them going to appear in play mode and only one time. I tried to duplicate the lines and you can see in the scripts but it dosent work…
any help?

the text script:

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

public class UIappeare : MonoBehaviour
{
    [SerializeField] private Text customText;
    [SerializeField] private Text customTextA;


    void OnTriggerStay(Collider other)
    {


        if (other.tag == "Player" && Input.GetKeyDown(KeyCode.Alpha1))
        {
            customText.enabled = true;
        }

        if (other.tag == "Player" && Input.GetKeyDown(KeyCode.Alpha2))
        {
            customTextA.enabled = true;
        }
    }

}

the fade script:

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

public class FadeOut : MonoBehaviour
{

    public Text withFade;
    public Text withFadeA;

    // Start is called before the first frame update
    void Start()
    {
        StartCoroutine(MovePlayer());
        IEnumerator MovePlayer()
        {
            yield return new WaitForSeconds(3f);

            withFade.canvasRenderer.SetAlpha(1f);
            withFadeA.canvasRenderer.SetAlpha(1f);

            fadeout();

        }

        // Update is called once per frame
        void fadeout()
        {
            withFade.CrossFadeAlpha(0, 2, false);
            withFadeA.CrossFadeAlpha(0, 2, false);
        }
    }
}

First thing I see is that the fadeout only does code in Start(), which only happens once.

Second problem is you are relying on checking GetKeyDown() from within an OnTriggerStay(). That won’t work 100% of the time. You can only reliably check GetKeyDown() in an Update() function.

Third thing is why are you putting MovePlayer and fadeout functions as local inside of Start()? Sure, I guess that’s legal, but just …odd. Make them peer functions to the Start().

As always the first thing you should reach for is the Debug.Log() statement: put in LOTS of those to display what your code is doing in real time. Nobody here can do that. It’s up to you. It will reveal amazing details about what your code is doing.

I understand but I’m very new in scripting and unity…what should I put instead of start?
How the code should look with the rest of the configuration you mentioned here?

UP