0
\$\begingroup\$

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);
        }
    }
\$\endgroup\$
1
\$\begingroup\$

Get a reference to RectTransform component instead of transform position. Just attach this script to your gameObject.

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

public class TextMoveFade : MonoBehaviour {

    public float Speed = 10;
    public float FadeOutSpeed = 0.1f;
    public Vector3 MoveToPosition = new Vector3 (0, -10);
    public float distanceOffset = 1;
    public RectTransform RectTransform;
    public Text CustomText;

    // Use this for initialization
    void Start () {
        StartCoroutine (MoveAndFade ());
    }

    IEnumerator MoveAndFade()
    {
        while (true) {
            RectTransform.position -= Vector3.up * Time.deltaTime * Speed;
            CustomText.color -= new Color(0,0,0,FadeOutSpeed);

            if(Vector3.Distance(MoveToPosition, RectTransform.position) < distanceOffset)
                break;

            yield return null;
        }
    }
}
\$\endgroup\$
4
  • \$\begingroup\$ Tried RectTransform. It returns the same position when I check via Debug, and also jumps the Text Y position to 1620, same as before. \$\endgroup\$ – kar Mar 13 '15 at 21:58
  • \$\begingroup\$ Added example code to answer. \$\endgroup\$ – 2600th Mar 13 '15 at 22:06
  • \$\begingroup\$ Applying this directly to an empty gameObject doesn't work as it has no reference to rectTransform. I modified the Text text to public, attached the text ui onto it and attached this script directly onto the text itself. The position is same as before within the camera view but the text doesn't appear now. Able to see color changing under the Inspector. \$\endgroup\$ – kar Mar 13 '15 at 22:23
  • \$\begingroup\$ Now you can attach the script to empty gameObject. \$\endgroup\$ – 2600th Mar 13 '15 at 22:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.