1. We've introduced thread tags, search within a thread and similar thread search. Read more here.
    Dismiss Notice
  2. We've released our first Timeline Experimental Preview, our new tool for creating cutscenes and more! To check it out click here.
    Dismiss Notice
  3. All Unity Pro perpetual license customers: your special offer to subscribe is ready! Click here.
    Dismiss Notice
  4. Unity 5.5 is now released.
    Dismiss Notice
  5. Check out all the fixes for 5.5 in patch releases 1 & 2.
    Dismiss Notice
  6. Unity 5.6 beta is now available for download.
    Dismiss Notice
  7. Get prepared for the Tizen Mobile App Incentive Program! Read more about the upcoming program here.
    Dismiss Notice
  8. Enter the Microsoft Developer Challenge for a chance to win prizes. Read more about it here.
    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
    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

    Joined:
    Apr 5, 2008
    Messages:
    26,595
    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
    Messages:
    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
    Messages:
    26,595
    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