Spawning moving text make it disappear overtime

I am trying to display a text when I click the letter ‘R’. When the text appears, it should slowly slide down on the y axis only, slowly disappear over 4 seconds and get disabled.

I am not able to find any clear guidance on the new UI. Most are just telling how to insert and move them on the Unity Editor and not able to find anything on scripting and the documentation doesn’t seem to have a clear description focused on UI scripting either (or maybe I am just that bad at reading it).

The following script makes the Text appear when I press the letter ‘R’. It does disappear after 4 seconds. But it doesn’t move down the y-axis and I am not able to assign the alpha value to the text incrementally to make it disappear over the 4 seconds.

Have commented the errors encountered below. I am testing this class separately on a separate Project altogether so no other interference. The Script is attached to an Empty Object on the scene.

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

public class TextScript : MonoBehaviour 
{
    public Text Text1;
    private float count;
    private float scrollDown, alpha, duration;

    void Start()
    {
        count = 0;
        scrollDown = 660;
        alpha = 4;
        duration = 1.5f;
        StartCoroutine(WaitAndPrint(2.0F));
    }

    void Update()
    {
        TextSpawn();
        KeyCount();
    }

    //spawn the Text on the screen when count is higher than 1
    //text should slowly move down on the y axis
    //same time its Color alpha should go down too and making it disappear and when alpha is zero, disable the text. 
    void TextSpawn()
    {
        if (count >= 1)
        {
            Text1.enabled = true;
            Text1.color = Color.black;
            Text1.text = "Text 1 !";
        }

        if (alpha > 0)
        {
            //if I use the variable scrollDown instead of 660, the Text will not appear on screen
            
            Text1.transform.position = new Vector3(748, scrollDown, transform.position.z);
            
            //this line of code will make the Text jump in the Y axis to 1620 even before I press 'R'
            //scrollDown += 10f;

            alpha -= Time.deltaTime / duration;
            
            //trying to set value to alpha which is not accepted. 
            //Text1.material.color.a = alpha;
        }
        else
        {
            Text1.enabled = false;
        }
    }

    //Using this to increment count and enable the Text
    void KeyCount()
    {
        if (Input.GetKeyDown(KeyCode.R))
        {
            count++;
        }
    }

    //Using this to find out the position of the text for testing
    IEnumerator WaitAndPrint(float waitTime)
    {
        yield return new WaitForSeconds(waitTime); 
        Debug.Log(Text1.transform.position);
    }
}

No offense, but I think it’ll be easier to show you how I’d do it instead of making your code work, it doesn’t look good.

First, to move UI elements use the RectTransform component, not the default Transform component. Get a reference on the Start method and use that reference later. The RectTransform component has a property called “anchoredPosition” that’s the Position X and Y you see in the inspector, but for anchors that do not show the X and Y position you might need to check the sizeDelta and rect properties too.

Next, you’re not changing the alpha of the Text’s color anywhere, you’re just changing an “alpha” variable that you declared in your script.

Another detail, since you know how to use coroutines, all the animation should be done in a coroutine, is much cleaner than using the Update.

Also, you might want to use the Lerp function to go from a value to another.

Here’s the code I wrote, I tested it and it’s working. Just add it to an empty GameObject in your hierarchy and set the Text reference to a Text game object from your Canvas.

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

public class TextScript : MonoBehaviour
{
	public Text Text;

	private float duration;
	private RectTransform rectTransform;
	private Vector2 textStartPosition, textEndPosition;
	private Color textStartColor, textEndColor;
	private Coroutine TextCoroutine;

	void Start() {
		rectTransform = Text.GetComponent<RectTransform>();
		textStartPosition = rectTransform.anchoredPosition;
		textEndPosition = new Vector2(textStartPosition.x,Screen.height/2);
		textStartColor = Text.color;
		textEndColor = new Color(textStartColor.r,textStartColor.g,textStartColor.b,0f);
		duration = 4f;
	}

	void Update()
	{
		if (Input.GetKeyDown(KeyCode.R))
		{
			if (Text.enabled)
				StopCoroutine(TextCoroutine);
			TextCoroutine = StartCoroutine(ShowText());
		}
	}

	IEnumerator ShowText() {
		Text.enabled = true;

		float elapsedTime = 0;

		while (elapsedTime < duration) {
			float t = elapsedTime / duration; //0 means the animation just started, 1 means it finished
			rectTransform.anchoredPosition = Vector2.Lerp(textStartPosition,textEndPosition,t);
			Text.color = Color.Lerp(textStartColor,textEndColor,t); 
			elapsedTime += Time.deltaTime;
			yield return null;
		}

		Text.enabled = false;
	}
}

I’m not sure what the “count” meant in your script so I removed it from mine.