Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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.

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

Difference between yield WaitForSeconds and yield new WaitForSeconds 1 Answer

Mysteries of yield 1 Answer

How to dispaly list of images from server on GUI? 1 Answer

How to add delay before showing GUI Button? 3 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges