- Home /
Reveal next letter on Input.anyKeyDown
Hey all,
I'm very new to code so please forgive my noobish ways. I'm trying to write a simple code that reveals the next letter in a paragraph of text with Input.anyKeyDown. Basically I want it to show one letter at a time no matter the key you press.
While searching for an answer I came across an AutoType script. With many failed attempts to edit it for my needs I decided to come here to ask for help.
using UnityEngine;
using System.Collections;
public class TextAuto : MonoBehaviour
{
public float letterPause = 0.05f; //NOT NEEDED
public GUIStyle font;
string message;
string text;
void Start (){
message = "Welcome new warrior! This is a blue screen, you must fight your way out! " +
"Without any hands or other parts. This may take some time...";
text = "";
//StartCoroutine(TypeText());
}
IEnumerator TypeText (){
foreach (char letter in message.ToCharArray())
{
text += letter;
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;
}
}
}
Cheers Adam
Answer by Glurth · Mar 10, 2015 at 09:05 PM
I see no need for a coroutine; it looks like you are trying to use the coroutine to time the new characters output, rather than base it on a keypress, as you said was desired.
I would try something like this (I have not complied the below, so it may have typos):
int counter;
void Start (){
message = "Welcome new warrior! This is a blue screen, you must fight your way out! " +
"Without any hands or other parts. This may take some time...";
text = "";
counter=0;
}
Update()
{
if(Input.anyKeyDown)
{
text += message[counter++];
}
}
Fantastic! that worked perfectly. Thanks, Glurth. Your help is very much appreciated.
Your answer
![](https://koobas.hobune.stream/wayback/20220613185708im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Get Text from an Input Field on one scene and use it as a text on another scene Unity c# 0 Answers
Display the same string in multiple text boxes 1 Answer
I have a problem with Unity UI Text 0 Answers
I need help with a Text Location script (Ho bisogno di aiuto con uno script di posizione del testo) 0 Answers
Making texts appear/disappear after having completed a task 0 Answers