1. Help us improve the editor usability and artist workflows. Join our discussion to provide your feedback.
    Dismiss Notice
  2. Unity 5.6 is now released.
    Dismiss Notice
  3. Check out all the fixes for 5.5 in patch releases 1 & 2.
    Dismiss Notice
  4. Get further faster with the Unity Plus Accelerator Pack, free for new Unity Plus subscribers for a limited time. Click here for more details.
    Dismiss Notice
  5. Learn how you'll soon be able to publish your games to China in four simple steps with Xiaomi. Sign up now for early access.
    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,596
    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,596
    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