script error: OnGUI() can not be a coroutine

Discussion in 'Scripting' started by lesfundi, Jun 17, 2009.

  1. lesfundi

    lesfundi

    New Member

    Joined:
    Jan 10, 2009
    Messages:
    628
    script error: OnGUI() can not be a coroutine -> this is
    the error i get with this script. Any idea what is wrong?


    var lockedGUI : GUITexture;
    var iconlevel1 : Texture2D;
    var iconlevel2 : Texture2D;

    function OnGUI () {
    if (GUI.Button (Rect (10,10, 100, 50), iconlevel1)) {
    print ("you clicked the icon");
    Application.LoadLevel("1");
    }

    if (GUI.Button (Rect (10,70, 100, 20), "Support/Manual")) {
    print ("you clicked the text button");
    }

    if (GUI.Button (Rect (10,90, 100, 50), iconlevel2)) {
    if (PlayerPrefs.GetInt("2", -1) != -1)
    {
    Application.LoadLevel("2");
    }
    else
    {


    lockedGUI.gameObject.active = true;
    yield new WaitForSeconds (3);
    Destroy (this);
    lockedGUI.gameObject.active = false;

    }
    }

    }
     
  2. Dreamora

    Dreamora

    Member

    Joined:
    Apr 5, 2008
    Messages:
    26,589
    you can not yield in OnGUI
    Also there is no reason for it, as OnGUI is called twice per frame (once for rendering, once for the input handling), independent if the previous one has ended or not.
     
  3. lesfundi

    lesfundi

    New Member

    Joined:
    Jan 10, 2009
    Messages:
    628
    so, how can i make it so it stay on the screen for a few second and than disssapear?
     
  4. Dreamora

    Dreamora

    Member

    Joined:
    Apr 5, 2008
    Messages:
    26,589
    You have a variable that tracks when the gui object has been started with drawing and after a given amount of time has passed since that point you do not draw it anymore.
    GUI stuff does not stay on screen, if you do not call the function anymore on the next frame, the gui object is just not present anymore