WaitForSeconds doesn't work in c#

How can i create this GUI in c# with WaitForSeconds ?

I want create a animation GUI

using UnityEngine;
using System.Collections;

public class ExitMenu : MonoBehaviour
{
    public Texture[] bigButtonSprite;
    private int i=0;
    void OnGUI()
    {
for (int i = 0; i < bigButtonSprite.Length-1; i++)
        {
GUI.Label(new Rect(Screen.width / 2 - bigButtonSprite[i].width / 2, bigButtonSprite[i].height * 2, bigButtonSprite[i].width, bigButtonSprite[i].height), bigButtonSprite[i]);
yield return new WaitForSeconds(1);
        }
}

I’m unsure if the OnGUI function can do this, but you could try:

IEnumertator OnGUI()
    {
for (int i = 0; i < bigButtonSprite.Length-1; i++)
        {
GUI.Label(new Rect(Screen.width / 2 - bigButtonSprite[i].width / 2, bigButtonSprite[i].height * 2, bigButtonSprite[i].width, bigButtonSprite[i].height), bigButtonSprite[i]);
yield return new WaitForSeconds(1);
        }

That should allow your code to use the yield properly. If not, then looking into a coroutine to handle the changing which texture is the current gui texture to display might work.

i wouldn’t put it in OnGUI, as it is called very frequently (i think once every frame).

LordAelfric
I write this code but i have this error “Script error: OnGUI() can not be a coroutine.”

please help me ?
it’s important for me

using UnityEngine;
using System.Collections;

public class ExitMenu : MonoBehaviour
{
    public Texture[] bigButtonSprite;
    private int i=0;
    private bool activated = true;
    
    void Start()
    {
         StartCoroutine(yourfunction());
     }

    void OnGUI()
    {

GUI.Label(new Rect(Screen.width / 2 - bigButtonSprite[i].width / 2, bigButtonSprite[i].height * 2, bigButtonSprite[i].width, bigButtonSprite[i].height), bigButtonSprite[i]);

        
}


IEnumerator yourfunction()
{
    for (int b = 0; b < bigButtonSprite.Length-1; b++)
       {
             i = b;
             yield return new WaitForSeconds(1);
        }
}

I’ve not tryed this, but I guess this work, say me if it works…

thank

Thanks KillerZ, got to it before I did.

I’m happy that it works :smile: