- Home /
Using coroutine to change current GUI
Hi, as the title try to say, I have to change my GUI, specifically, a GUITexture, from time to time. For example: in the first 5 seconds, I have to display the GUITexture1. After this, for another 5 seconds, I have to show the GUITexture2. I intend to display this GUITextures to make something like a "Introduction".
So, I've tried to do something like this:
void OnGUI ()
{
if (messageGUI)
{
if (textOpt == "Ligado")
{
message = Resources.Load("Levels/General/generalSpeech1") as Texture;
StartCoroutine(showMessage(message, new Vector2(tabItem, Screen.height-avatarSize-2*tabItem-messageSize.y), messageSize, time));
message = Resources.Load("Levels/General/generalSpeech2") as Texture;
StartCoroutine(showMessage(message, new Vector2(tabItem, Screen.height-avatarSize-2*tabItem-messageSize.y), messageSize, time));
message = Resources.Load("Levels/General/generalSpeech3") as Texture;
StartCoroutine(showMessage(message, new Vector2(tabItem, Screen.height-avatarSize-2*tabItem-messageSize.y), messageSize, time));
}
}
}
IEnumerator showMessage (Texture message, Vector2 position, Vector2 size, float time)
{
messageGUI.enabled = true;
messageGUI.texture = message;
messageGUI.name = "Message";
messageGUI.pixelInset = new Rect(position.x, position.y, size.x, size.y);
yield return new WaitForSeconds(time);
messageGUI.enabled = false;
}
But I've noted that this don't solve my problem, 'cause the OnGUI function are always running and the next command don't wait the previous to finish before starts running. I know why, but I don't figure out how could I do what I want. Any ideas?
Thanks in advance.
I suppose implementing your own timers with Time.deltaTime
should work.
Answer by paulaceccon · Dec 26, 2012 at 04:33 PM
Yeah, thks @asafsitner, and @cawas, for helping even outside the forum.
I could solve my problem with this script:
void Update(){
introduction ();
if(displayMessage){
Debug.Log(timer+" "+currentMessage);
timer += Time.deltaTime;
if(timer <= time){
messageGUI.enabled = true;
messageGUI.texture = message;
messageGUI.pixelInset = new Rect(position.x, position.y, messageSize.x, messageSize.y);
}
else
{
messageGUI.enabled = false;
displayMessage = false;
timer = 0f;
currentMessage ++;
}
}
}
//Levels
void introduction ()
{
if (textOpt == "Ligado")
{
if (currentMessage == 1)
{
message = Resources.Load("Levels/General/generalSpeech1") as Texture;
position = new Vector2(tabItem, Screen.height-avatarSize-2*tabItem-messageSize.y);
displayMessage = true;
}
else if (currentMessage == 2)
{
message = Resources.Load("Levels/General/generalSpeech2") as Texture;
position = new Vector2(tabItem, Screen.height-avatarSize-2*tabItem-messageSize.y);
displayMessage = true;
}
else if (currentMessage == 3)
{
message = Resources.Load("Levels/General/generalSpeech3") as Texture;
position = new Vector2(tabItem, Screen.height-avatarSize-2*tabItem-messageSize.y);
displayMessage = true;
}
}
}
Great to hear! You should accept your answer, then. :)
Your answer
![](https://koobas.hobune.stream/wayback/20220613085843im_/https://answers.unity.com/themes/thub/images/avi.jpg)