FixedUpdate and OnGUI doubts

Hello Guys, I’m new in coding and have some troubles , I hope you can help me :slight_smile:

I’m trying to code a dialog system for a visual novel game (or games). It’s very simple, when you press right key shows you the next text and when you press the left key it shows you the prev text, but I’d like the text to be shown letter by letter (or word by word) and not all the complete text you can see it here:

var dialogos : String[]; //This variable is for adding the Texts via Inspector.
private var i = 0; //This is the number of the node on the prev Array.
var e = 0; //This is what I want to use for the "text spelling".

function FixedUpdate () {	
	
	if(Input.GetKeyDown("right")){
		i++;
	}else if(Input.GetKeyDown("left")){
		i--;
	}else if(i < 0){
		i = 0;
	}else if(i > dialogos.Length){
		i = dialogos.Length;
	}
}

function OnGUI () {	

	e++;
	dialogos[i].Length = e;
	GUI.Box(Rect(50,10,300,50),dialogos[i]);
}

As you can see I can’t make the text appear as I want because String.Length is Read-Only, do you know how to do something like this? This is an example of how do I want the text:

I hope you can help me, and Thank you for all. :slight_smile:

First of all why do you use FixedUpdate for input checks?

Second of all, use a coroutine for it. So you use a string to show the GUI text in OnGUI, and in a for/foreach loop you add one letter at the time, having a WaitForSeconds(some value) running inside the loop.

I use FixedUpdate because if I use Update or OnGUI functions for Input Checks the variable “i” in the array “dialogos*” will count all vectors until the last and no one by one when I press the Right Key.*
When you say “One Letter at the time” do you mean in every new dialog texts, so a new text should need to be done “letter-by-letter” in a new array?
Thanks for reply.

Instead of displaying the string directly, display dialogos*.Substring(0, desiredLength) where desiredLength is a number you can increase over time.*

Wow it worked very good :open_mouth: I just have a last question: How should I do for delaying the speed of every character to appear on screen? I tried with yield WaitForSeconds(); but it says that I can’t put a courutine in an Update or OnGUI function.

This is the new code in the same 3D Text (I’m having some troubles with OnGUI but I’ll fix them later):

var dialogos : String[];
var tex3d : TextMesh;
var i = 0;

function Update () { //I changed FixedUpdate for this and curiously it works very good without counting every vector;

	if(Input.GetKeyDown("right")){
		i++;
			for (var e = 0;e < dialogos[i].Length;e++){ //This is for making the text appear letter-by-letter;
				Debug.Log(e + tex3d.text);
				tex3d.text = dialogos[i].Substring(0,e);
                                yield WaitForSeconds(0.1); //Here is the error, as I suppose you know;
			}
		
	}else if(Input.GetKeyDown("left")){
		i--;
		tex3d.text = dialogos[i];
	}
}

Sorry for too many questions, as I said I’m a bit new in programming but I’m catching everything you say and also google by myself.

Again, thanks very much for your responses. :slight_smile:

Google Coroutines in unity :wink:

I wouldn’t use a coroutine for this. They’re awesome, but they’re pretty heavy-weight for this, especially considering that you’re already calling an Update() function which gives you everything you need.

Personally, I’d create a float called “characterAddTimer” or similar. Then each update do something like…

characterAddTimer += Time.deltaTime;
while (characterAddTimer >= 0.1f) {
    charactersToDisplay++;
    characterAddTimer -= 0.1f;
}

Replace the 0.1s with the amount of time you want each character to take, and you should be golden. (For bonus points, replace the 0.1s with a variable “secondsPerCharacter” or something so that you can control it more elegantly.)

Why use a coroutine when you’re already using an Update() function and can use a simple timer variable?