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);
}
}