- Home /
Character Collision Trigger Text Message Help!
Hey guys, I am new to scripting and do not know how to go about creating this. That said, I want a text message to show up when my character walk's thru a collider and as he exit's out, the text should disappear. And apart from this, how can I make the text disappear after a certain amount of time?
Well, this is what I've done so far. Now the text appears after a certain interval and does not disappear. Would be really nice If someone could help me out. Thank you very much in advance.
using UnityEngine;
using System.Collections;
public class mainText : MonoBehaviour
{
public float letterPause = 0.05f;
public AudioClip sound;
public GUIStyle font;
string message;
string text;
void Start ()
{
message = "Hello, World!";
StartCoroutine(TypeText());
}
IEnumerator TypeText ()
{
float timeToWait = 2;
float incrementToRemove = 0.5f;
while(timeToWait > 0)
{
yield return new WaitForSeconds(incrementToRemove );
timeToWait -= incrementToRemove;
}
foreach (char letter in message.ToCharArray())
{
text += letter;
if (sound)
audio.PlayOneShot (sound);
yield return 0;
yield return new WaitForSeconds (letterPause);
}
}
void OnGUI()
{
GUI.Label(new Rect(0, 0, 256, 1024), text, font);
}
void Update()
{
if(Input.GetKeyDown (KeyCode.Return))
{
StopAllCoroutines();
text = message;
}
}
}
Comment
Best Answer
Answer by FL · Feb 24, 2013 at 05:22 PM
public float textPausedSeconds = 5.0f;
float textDisappearTime = -1.0f;
//Both declared as global variables
//code that record text
textDisappearTime = Time.time + textPausedSeconds;
void OnGUI()
{
if(Time.time<=textDisappearTime)
GUI.Label(new Rect(0, 0, 256, 1024), text, font);
}