• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by androids · Sep 22, 2013 at 07:55 PM · yieldwaitforsecondsongui

yield WaitForSeconds in OnGUI

I have a GUI button. If I press the button, I have to wait for 1 second then should go to Level 1.

But now my problem is that I can't use yield WaitForSeconds(1);. It gives me this error.

This is by the way my JS:

 var gui01 : Texture2D;
 var gui01rollover : Texture2D;
 
 private var correctedMousePosition : Vector2;
  
 function OnGUI ()
 {
     correctedMousePosition = Vector2 (Input.mousePosition.x, (Screen.height-Input.mousePosition.y) );
     if ( Rect(101,303,88,88).Contains(correctedMousePosition) )
     {
         GUI.DrawTexture( Rect(101,303,88,88) , gui01rollover);
         if( Input.GetMouseButtonUp(0) )
         {
             yield WaitForSeconds(1);
             Application.LoadLevel("S1_level01");
         }
     }
     else
     {
         GUI.DrawTexture( Rect(101,303,88,88) , gui01);
     }
 }

And this is the error Unity3D gives me; Script error: OnGUI() can not be a coroutine.

Can someone help me with this problem please?

Thanks in advance.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

4 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by YoungDeveloper · Sep 22, 2013 at 08:28 PM

Here's a similar question i answered, but instead of ongui, it's update there. http://answers.unity3d.com/questions/541018/wow-my-enemies-are-pretty-strong.html

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
1

Answer by Eric5h5 · Sep 22, 2013 at 08:07 PM

OnGUI runs every frame like Update, and can't be delayed in any way. You can launch a separate coroutine from OnGUI instead (but you must take steps to ensure that it will only be launched once).

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by ItsK4rma · Sep 22, 2013 at 08:48 PM

I've modified some code for you. A very basic way to solve this is to add a boolean (in this example I called it loadingLevel, and set it to false. Then when you add a simple if statement, that says if loadingLevel is false, then it is okay to enter the coroutine LoadLevel(), but before doing so, we set the loadingLevel boolean to true, this way the next time OnGUI runs, it does not enter that if statement, thus calling the LoadLevel() function again. Then the LoadLevel() function simply waits 1 second, then loads the level you specify.

 var gui01 : Texture2D;
 var gui01rollover : Texture2D;
 var loadingLevel : boolean = false;
  
 private var correctedMousePosition : Vector2;
  
 function OnGUI ()
 {
     correctedMousePosition = Vector2 (Input.mousePosition.x, (Screen.height-Input.mousePosition.y) );
     if ( Rect(101,303,88,88).Contains(correctedMousePosition) )
     {
         GUI.DrawTexture( Rect(101,303,88,88) , gui01rollover);
         if( Input.GetMouseButtonUp(0) )
         {
            if (!loadingLevel)
            {
               loadingLevel = true;
               LoadLevel ();
            }
         }
     }
     else
     {
         GUI.DrawTexture( Rect(101,303,88,88) , gui01);
     }
 }
 
 function LoadLevel () {
     yield WaitForSeconds(1);
     Application.LoadLevel("S1_level01");
 }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by androids · Sep 22, 2013 at 08:54 PM

I fixed my problem. For those who have the same problem I had with OnGUI. You can find the right JS here.

 var gui01 : Texture2D;
 var gui01rollover : Texture2D;
 
 private var correctedMousePosition : Vector2;
 
 function OnGUI ()
 {
     correctedMousePosition = Vector2 (Input.mousePosition.x, (Screen.height-Input.mousePosition.y) );
     if ( Rect(101,303,88,88).Contains(correctedMousePosition) )
     {
         GUI.DrawTexture( Rect(101,303,88,88) , gui01rollover);
         if( Input.GetMouseButtonUp(0) )
         {
             GoScene();
         }
     }
     else
     {
         GUI.DrawTexture( Rect(101,303,88,88) , gui01);
     }
 }
 
 function GoScene()
 {
     yield WaitForSeconds(1);
     Application.LoadLevel("S1_level01");
 }
Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image galaboy · Nov 21, 2013 at 12:18 PM 0
Share

i couldn't able to understand. i header that waitforseconds can be used only in IEnumerator funtion, is the above code possible.
need help.

avatar image Eric5h5 · Nov 21, 2013 at 05:44 PM 0
Share

@galaboy: In the above code, WaitForSeconds is in an IEnumerator function.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

19 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Function On GUI 2 Answers

yield WaitForSeconds makes GUI button disappear 1 Answer

C# yield waitforseconds 3 Answers

How can I make a variable false for a period of time? 3 Answers

What is wrong with this use of WaitForSeconds? 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges