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 andreas777 · Mar 10, 2014 at 10:41 PM · guifunctionyield

A yield function for function Update?

I need to add a yield new WaitForSeconds(2); where the application loads the level...How will I do this? :)

 var timer: float = 30; 
 var isFinishedLevel : boolean = false;
  
 function Update()
 {
     if (!isFinishedLevel) 
     {
        timer -= Time.deltaTime;  
     } 
  
     if (timer > 0)
     {
        guiText.text = timer.ToString("F0");
     } 
     else
     {
         GameMaster.currentScore +=1;
         if (GameMaster.currentScore > PlayerPrefs.GetInt("ScoreScript")){
             PlayerPrefs.SetInt("ScoreScript", GameMaster.currentScore);
         }
  
            Application.LoadLevel("Level5");
 
     }
 }
Comment
Add comment · Show 1
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 Owen-Reynolds · Mar 10, 2014 at 11:02 PM 0
Share

Hmm...you already have a 30 second timer. Yielding for two more seconds would be the same thing as just starting the timer at 32.

Or do you want to load a new level then wait two seconds? Or wait 30 seconds, freeze the game for two seconds, then load?

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by robertbu · Mar 10, 2014 at 10:57 PM

'yield' cannot be used in Update(), so to get your new scene, pull the LoadLevel() into a new function:

 function LoadNextLevel() {
     yield WaitForSeconds(2);
     Application.LoadLevel("Level5");
 }

Then you would call LoadNextLevel() on line 22 instead of the Application.LoadLevel().

Note there as alternate, you could leave out the 'yield', and use Invoke() to run LoadNextLevel() in two seconds.

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 andreas777 · Mar 10, 2014 at 11:10 PM 0
Share

hello, I tried putting a new function and putting it there but it also didn't work. can anyone please change accordingly the complete script for me as I tried a lot of options and cant move on. Basically I would like to freeze the game for two seconds, then load the new level.

avatar image robertbu · Mar 10, 2014 at 11:17 PM 0
Share

This is what I had in $$anonymous$$d. Note I added a 'loading' variable so that LoadNextLevel() only gets called once.

 #pragma strict
 
 var timer: float = 30; 
 var isFinishedLevel : boolean = false;
 var loading = false;
  
 function Update()
 {
     if (!isFinishedLevel) 
     {
        timer -= Time.deltaTime;  
     } 
  
     if (timer > 0)
     {
        guiText.text = timer.ToString("F0");
     } 
     else
     {
         Game$$anonymous$$aster.currentScore +=1;
         if (Game$$anonymous$$aster.currentScore > PlayerPrefs.GetInt("ScoreScript")){
            PlayerPrefs.SetInt("ScoreScript", Game$$anonymous$$aster.currentScore);
         }
         if (!loading)
             LoadNextLevel();
     }
 }
 
 function LoadNextLevel() {
     loading = true;
     yield WaitForSeconds(2);
     Application.LoadLevel("Level5");
 }

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

21 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

Related Questions

How do I create a GUI on collision detection? 1 Answer

Yield function goes slow sometimes/differently? 2 Answers

GUI.Window function with variables? 3 Answers

How can I use a yeild during an udate? 1 Answer

Im a bit confused on a simple script 1 Answer


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