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 uanmanarmy · Jul 21, 2014 at 12:06 PM · not workingienumeratorstartcoroutineworking

StartCorutine not Working

 void OnGUI()
     {
         if(GUI.Button(new Rect(20,20,120,120), "New Level"))
         {
             StartCoroutine(Waittt());
             if(!LevelGo)
                     boardObject.CreateGemsGrid();
 
             if(LevelGo){
             //boardObject.DeleteGemsGrid();
             //boardObject.CreateGemsGrid();
                 
             }
             LevelGo = true;
             menuSriptObj.LevelList();
             autoObject.DeleteItems();
             autoObject.CreateItems();
 
             // % will make sure to go from beginning when newlevel == numbersOfLevels - 1
             newLevel = (newLevel+1) % numbersOfLevels;
             print (newLevel);
         }
     }
 
     public IEnumerator Waittt()
     {
         yield return new WaitForSeconds(10);
     }

This is my script.

Usually after I press new Level I have to wait for 10 seconds and the the bellow code will be runned, right or not?

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

3 Replies

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

Answer by FrosTech · Jul 21, 2014 at 12:23 PM

This is a typical error any beginner faces, as I did too.

OnGUI runs every frame, that is it runs atleast (1 to counting) times or more each frame.

So, OnGUI just runs every frame.

This is a simple explanation on OnGUI(). Not related to your question, but its better to know, right??


Coming to your rescue!!

The Waitt() method will run on the next frame, not the current one!

You can verify that by putting a Debug.Log("Print it") within Waittt() method.

It'll print right after 10 seconds just as you want!!

 void OnGUI()
     {
         if(GUI.Button(new Rect(20,20,120,120), "New Level"))
         {
             StartCoroutine(Waittt());
         }
     }
  
     public IEnumerator Waittt()
     {
         yield return new WaitForSeconds(10);
         newLevel();   // Includes the task you want to do after 10 seconds have passed.
     }

     public void newLevel()
     {
           if(!LevelGo)
                     boardObject.CreateGemsGrid();
  
             if(LevelGo){
             //boardObject.DeleteGemsGrid();
             //boardObject.CreateGemsGrid();
  
             }
             LevelGo = true;
             menuSriptObj.LevelList();
             autoObject.DeleteItems();
             autoObject.CreateItems();
  
             // % will make sure to go from beginning when newlevel == numbersOfLevels -1
             newLevel = (newLevel+1) % numbersOfLevels;
             print (newLevel);
         }


This should solve your issue for now.

Comment
Add comment · Show 1 · 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 uanmanarmy · Jul 21, 2014 at 12:32 PM 0
Share

thank you, I got it now )

avatar image
2

Answer by AyAMrau · Jul 21, 2014 at 12:17 PM

No, the coroutine will wait for 10 seconds and then exit cause it's empty except for the yield statement. If you had any code under it would only be run after 10 seconds.

However a coroutine is a separate thread and while it will wait, the code in the calling function will continue to run.

Therefore any code you want to delay running should be moved to the coroutine after the yield statement. But, beware that unless you disable / destroy the button once it has been clicked once a user will be able to continue doing that and start new coroutine every time (which is probably not what you want).

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 Supershandy · Jul 21, 2014 at 12:20 PM

If you're looking to create a new level every ten seconds, you would be better running an InvokeRepeating routine rather than a StartCoroutine like this.

 if(GUI.Button(new Rect(20,20,120,120), "New Level"))
         {
             InvokeRepeating(Waittt(),0, 10f);


And then move everything under the StartCoroutine to Waaaaait() like this.

 void OnGUI()
     {
         if(GUI.Button(new Rect(20,20,120,120), "New Level"))
         {
             InvokeRepeating(Waittt(), 0, 10f);
         }
     }
 
 Public void Waittt ()
 
 {
             if(!LevelGo)
                     boardObject.CreateGemsGrid();
  
             if(LevelGo){
             //boardObject.DeleteGemsGrid();
             //boardObject.CreateGemsGrid();
  
             }
             LevelGo = true;
             menuSriptObj.LevelList();
             autoObject.DeleteItems();
             autoObject.CreateItems();
  
             // % will make sure to go from beginning when newlevel == numbersOfLevels - 1
             newLevel = (newLevel+1) % numbersOfLevels;
             print (newLevel);
         }
     }
  
 

You'll have to forgive some of the coding as I'm at work, but that would be the general jist of what you're trying to achieve, Unity Script Reference qould give you better details on uaing InvokeRepeating()

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 Supershandy · Jul 21, 2014 at 12:28 PM 0
Share

I should point out that InvokeRepeating in this case would start as soon as the GUI button is pressed (0) and repeat every 10 seconds (10f)

avatar image uanmanarmy · Jul 21, 2014 at 12:31 PM 0
Share

okey I got it now, thank's

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

24 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 avatar image avatar image avatar image avatar image avatar image

Related Questions

A node in a childnode? 1 Answer

Programming Question with WaitForSeconds 2 Answers

Fire rate for gun script, c# (not u/s) 1 Answer

WaitForSeconds does not work 0 Answers

Object Not Instantiating After Being Destroyed 0 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