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 QQHughes · Apr 05, 2012 at 07:46 AM · coroutineyielddelayrespawnsyntax

Inserting a delay

 var lowerBody:GameObject;
 function Update (){
     if (Health.health==0){
         //Where the delay should be
         Health.health=100;
         transform.position=Vector3(0,8,0);
         lowerBody.transform.position =Vector3(0,0.5,0);
     }
 }

How can I delay the function 5 seconds at this point? I understand that I should likely be using while(){ and yield WaitForSeconds, but I can't figure out exactly how the syntax should work.

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

2 Replies

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

Answer by fafase · Apr 05, 2012 at 08:49 AM

Remember you need to put it in a coroutine that you call in the update.

This below is bad and won't work as the update needs to freely run.

 var lowerBody:GameObject;
 function Update (){
     if (Health.health==0){
         yield WaitForSeconds(5); // here this is wruuuung
        Health.health=100;
        transform.position=Vector3(0,8,0);
        lowerBody.transform.position =Vector3(0,0.5,0);
     }
 }

Proper way:

 var lowerBody:GameObject;
     function Update (){
         if (Health.health==0){
             WaitHere();  // you call
         }
     }
 
    
      function WaitHere(){
         Health.health=100;
         yield WaitForSeconds(5); // the function waits 5 sec before doing what is next
                // in the meanwhile everything else keeps going.
             transform.position=Vector3(0,8,0);
             lowerBody.transform.position =Vector3(0,0.5,0);
             }
Comment
Add comment · Show 5 · 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 Bazsee · Apr 05, 2012 at 09:08 AM 0
Share

wouldn't that script call the waithere() function for multiple times for 5 seconds? once per update?

avatar image Bazsee · Apr 05, 2012 at 09:21 AM 0
Share

maybe you should use a "respawning" or such variable, which turns to true when the health drops to 0, and in the Update() if statement you make a check if it's false so it wouldn't call the WaitHere() multiple times. Then in the WaitHere() function you should set it back to false after setting the Health back to 100.

avatar image fafase · Apr 05, 2012 at 09:52 AM 0
Share

I edited the answer (it should come up later on), actually yes you can do all that or simply put the health=100 before the yield so that the if statement is false. Now if he wants the health to wait before changing back to 100, he can do the same process.

avatar image Bazsee · Apr 05, 2012 at 09:57 AM 0
Share

And maybe a fadeout-fadein would be a good idea until the repositioning and health changing is done.

avatar image fafase · Apr 05, 2012 at 10:03 AM 0
Share

The question is about waiting, fade in-fade out is a material issue that was not asked by the user.

avatar image
0

Answer by DaveA · Apr 05, 2012 at 07:49 AM

It's right here: http://unity3d.com/support/documentation/ScriptReference/WaitForSeconds.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

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

restart level when dead with delay 3 Answers

Coroutine not running after yield return new WaitForSeconds 3 Answers

How to delay a respawn? 1 Answer

WaitForSeconds Question 2 Answers

why does Coroutine 'yield return 0' sometimes take a lot of frames before it continues 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