Wiki typewriter script and GUI 2.0

Hello All,

I have this script using the wiki’s typewriter script.
I was trying to use it so I can output a text on a GUI.Box or Label.
Since the wiki script requires a GUIText component I used a GUIText component instead of an empty one and slap this code to it.

/* Test GUI Scripts */

var word = "this is a test";
var myFont : GUISkin;
var letterPause = 0.2;
var sound : AudioClip;

var test = true;
function OnGUI () 
{
	
	GUI.BeginGroup(Rect(Screen.width/2-256,Screen.height/2-256,512,512));
	
	GUI.Box(Rect(0,0,512,512),"");
	
	GUI.skin = myFont;
	GUI.Label(Rect(10,2,200,18),GUIContent(guiText.text));
	GUI.EndGroup();
		
	
	
}

function Update()
{
	if(test == true)
	{

	TypeText();
	test = false;
	}
}


function TypeText() 
{
	//yield WaitForSeconds(4);
	for (var i = 0; i < word.Length; i++) 
		{
			guiText.text += word[i];
			if (sound)
			audio.PlayOneShot (sound);
			yield WaitForSeconds (letterPause);
		}
	//test = false;	
}

The problem is if the GUIText component transform is at 0.5,0.5,0.0…
it is also typing the string on that location(because I think it is attached to a GUIText component).

See my attached image.

I fixed this by putting the transform into some location far far away.

Is there a better way of doing this other than my hocus focus approach?

Thanks,
Ray

Edit: I meant Hello :sweat_smile:

60413--2210--$guitest_651.jpg

You may have figured this out already, but you should just use an empty Game Object if you’re going to use a Box or Label from the GUI. The wiki script probably used the GUIText because there was no GUI before Unity 2.0.

lgoss007

Yes, I was using an empty GO at first, but using it I get an error that guiText.text requires a GUITexture. What I was trying to figure out is “How can I do this typewritter effect” on a GUI 2.0

Thanks,
Ray

Instead of assigning guiText.text with the character string, just pass it to a variable that you pass into a GUI.Label. Since OnGUI is updated immediately, it will always reflect the changes in your variable that is updated in the TypeText method.

Cheers
Shaun

EDIT: If you’ve created a GUIText GO, I suggest dumping it, creating an empty GO and dropping this script on it, then updating the code.

Ah, I missed that guiText.text part. You can just fix that by doing what Shaun says. Code would be (comments included on changes):

/* Test GUI Scripts */ 

var word = "this is a test"; 
var myFont : GUISkin; 
var letterPause = 0.2; 
var sound : AudioClip; 
private var wordLabel : String; // Add this as the display text for our typewriter

var test = true; 
function OnGUI () 
{ 
   GUI.BeginGroup(Rect(Screen.width/2-256,Screen.height/2-256,512,512)); 
    
   GUI.Box(Rect(0,0,512,512),""); 
    
   GUI.skin = myFont; 
  // Change the content to our new wordLabel
   GUI.Label(Rect(10,2,200,18),GUIContent(wordLabel)); 
   GUI.EndGroup(); 
} 

function Update() 
{ 
   if(test == true) 
   { 
     TypeText(); 
     test = false; 
   } 
} 

function TypeText() 
{ 
   //yield WaitForSeconds(4); 
   for (var i = 0; i < word.Length; i++) 
      { 
        // set the word label text one at a time
         wordLabel += word[i]; 
         if (sound) 
         audio.PlayOneShot (sound); 
         yield WaitForSeconds (letterPause); 
      } 
   //test = false;    
}

lgoss007 shaun

Thank you, Works perfect

Ray