Assistance with Retro Style text

Hey, ladies and gents! I have a quick question. I’m trying to make my text which the player would read type out on to the screen in a retro fashion each frame. e.g: “H”, “He”, “Hel”, “Hell”, “Hello”; where each frame shows the next index of the string. I was attempting to mess with the functions within the code, however I came up with nothing that the system will accept. I looked this up for quite a while and couldn’t find the answer, so I decided to ask!
An example of what I want is something like this:

private string myText;
private string displayText;
private TextMesh myTxtMesh;

void Start () {
myTxtMesh = GetComponent<TextMesh>();
}

//Constantly update the string to display what needs to be displayed via textMesh:
void Update () {
myTxtMesh.text = displayText;
//Set what the string actually is:
myText = "this is a test.";
//Count the string letters and assign them:
	for (i = 0; i <= myText.Length; i++){
		displayText[i] += myText[i];
	}
}

(myText is referencing a TextMesh)
Also please note that I just typed this out here on the forum willy-nilly based on things that I’ve tried. I don’t expect it to be accurate.

The mistake you did is writing it in for loop.
When you write it in a loop that means you are asking the compiler to run the whole logic inside the loop in a single frame.
Instead you can to use the update function in a different way. :slight_smile:

Just to start from scratch, update function run every frame. That means it makes sure that every logic written inside the body will be executed before the frame is drawn on the screen. Once the frame is drawn then update is called again, and the amount of time it took since the start of previous update function execution to current is saved in Time.deltaTime.

So in this code we are gonna use the deltaTime and update frame to create the effect you wanted.

To do this we gonna need a global variable to save the current length (index) of the string you wanted to display at a given particular frame.

private int retroAnimatedTextIndex = 0;

and also need another global variable to save the text you wanted to show

private string retroStyleAnimatedText = "";

Since I started typing a long reply, I thought I’ll show you a better OOPs way of doing this so that you can reuse this function :smile:
So a function that takes string as parameter and can be reused as many time as you want.
Make the function public so that you can call it from other classes also.

// string to show the retro animated effect
public void ShowRetroAnimatedText( string pText ){
	retroAnimatedTextIndex = 0;
	retroStyleAnimatedText = pText;
	
	// resetting the text inside TextMesh
	myTxtMesh.text = "";
}

And now the magic happens in update function.

// a temparory variable to save the time elapsed between last letter that appeared
private float retroStyleAnimationDeltaTime = 0.0f;

// amount of delay between each letter to appear
private float const _RETRO_STYLE_DELAY_BETWEEN_LETTERS_ = 0.5f;

void Update () {
	// checking to make sure that the index is less than the text length
	if( retroAnimatedTextIndex < retroStyleAnimatedText.Length ){
		// incrementing the delta time
		retroStyleAnimationDeltaTime += Time.deltaTime;
		// checking if the time elapsed between last letter apperance and now is greater than delay
		// if it is greater then its time to show next letter
		if( retroStyleAnimationDeltaTime >= _RETRO_STYLE_DELAY_BETWEEN_LETTERS_ ){
			// resetting the delta time
			retroStyleAnimationDeltaTime = 0.0f;
			retroAnimatedTextIndex++;
			
			// since myTxtMesh is a text mesh it can retain the text
			// so applying the text once is enough
			myTxtMesh.text = retroStyleAnimatedText.Substring(0, retroAnimatedTextIndex);
		}
	}
}

And using this is a piece of cake.

ShowRetroAnimatedText("Hello World");

You can change the amount of delay, play around with values until you are satisfied with the output. :slight_smile:
Hope this usage of update function helps you in your future coding.
Good luck. :slight_smile:

Learn how to coroutine

string someText = "";
string fullText = "Type me!";

IEnumerator TypeText (){
  for(int index =0; index< fullText.Length; index++){
    someText += fullText[i];
    yield return new WaitForSeconds(0.1f);
  }
}

void Start () {
  Start StartCoroutine (TypeText());
}

void OnGUI(){
  GUILayout.Label(someText);
}

Thanks so much for both of your replies! Your help is much appreciated!!