How to draw GUI Text from code

Hey guys,

I was wondering, how do you draw some GUI text (or any GUI object for that matter) on the screen from Javascript. I know how to use the Time.time method to display it after a number of seconds but I want to be able to remove it after a number of seconds as well…

And also, it would be good if I was able to choose the font that this GUI text would use!!!
Thanks

-Grady

I recommend using an array of strings for your subtitles.

var waitTime : float;
var seconds : int; 
var timer : float;    
var f : Font;
var rectplayer1 : Rect[];
var rectplayer2 : Rect[]; //just keep making new arrays for every amount of subtitles you want.
var s1 : String[];
var s2 : String[]; //same here
var cur1 : int;
var cur2 : int; //and here

function Update() {
	waitTime = (Time.deltaTime * seconds);
	while(timer < waitTime) {
		timer += Time.deltaTime;
	}
	if(timer >= waitTime) {
		cur2++;
		timer = 0;
	}
}

//vars and function for gui code here in the same script:


    
function OnGUI() {
    	GUI.skin.label.font = f;
        GUI.Label(rectplayer1[cur1],s1[cur2]); //change cur1 between two or three rects and continuously increment cur2 to get an illusion of dialogue.
}

See? make one GUI.Label for each character’s subtitle you want, with its own string array, and it’s own original cur value and after that character is “done talking,” increment cur to go to the next subtitle. And in case you don’t know already, to use an array, set the size to however many elements (vars) you want, and then set those values to whatever you want, via the inspector, or code.

If you have any more questions, leave another comment. :slight_smile: I hope I helped!