- Home /
Changing displayed label in OnGUI from another function
Hi there,
I am creating a Dialogue-System. In OnGUI I am drawing a Label with the variable talkText in it.
I a function named talking() I am setting text to this talkText variable, getting the text from another object of the scene. The text is saved sentence by sentence inside an array. Now I am trying to assign the sentences one after another to talkText. But my OnGUI displays just the first sentence and after that nothing changes. Any idea?
function OnGUI(){
if(switchTalk){
GUI.Label (Rect (Screen.width /2 , 5, 50, 33), talkText, style);
talking();
}
}
function talking(){
// getting length of array, the number of sentences
var talkLength : int = gameObject.Find(talkTo.name).GetComponent(dialogue).dialogOption1.Length;
for(var talkCount : int = 0 ; talkCount < talkLength ; talkCount++ ){
//this is where I change talkText, but in OnGUI() it's not affected
talkText = gameObject.Find(talkTo.name).GetComponent(dialogue).dialogOption1[talkCount];
yield WaitForSeconds (2);
}
talkTo = null;
switchTalk = false;
}
In Update():
if (hit.collider.tag == "NPC" && !switchTalk){
if(Input.GetButtonUp("Fire1")){
switchTalk = true;
talkTo = hit.collider.gameObject;
}
Personaly I prefer GuiText object, then you could have all your sentences in an array. Then when you press the mouse, you increase the index of the array and call only when you change the text
guiText.text = str[i];
By the way your original problem was calling talking multiple times per frame - (from OnGUI) it was always initializing to the first string.
I thought talking() is just called once, because I am turning it off with switchTalk after the first time ...
Ah yes - in which case there was no guarantee it would ever draw - OnGUI is called multiple times per frame with different events - if logic/function calling like that can be a nightmare. Best to only do it if Event.current.type == EventType.repaint