Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

script error: OnGUI() can not be a coroutine

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

  1. lesfundi

    lesfundi

    Joined:
    Jan 10, 2009
    Posts:
    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

    Joined:
    Apr 5, 2008
    Posts:
    26,601
    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

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

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,601
    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