x


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.

more ▼

asked Apr 05 '12 at 07:46 AM

QQHughes gravatar image

QQHughes
6 3 5 6

(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

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);
            }
more ▼

answered Apr 05 '12 at 08:49 AM

fafase gravatar image

fafase
11.1k 11 15 45

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

Apr 05 '12 at 09:08 AM Bazsee

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.

Apr 05 '12 at 09:21 AM Bazsee

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.

Apr 05 '12 at 09:52 AM fafase

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

Apr 05 '12 at 09:57 AM Bazsee

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

Apr 05 '12 at 10:03 AM fafase
(comments are locked)
10|3000 characters needed characters left
more ▼

answered Apr 05 '12 at 07:49 AM

DaveA gravatar image

DaveA
26.8k 153 171 257

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

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

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x348
x336
x186
x182
x71

asked: Apr 05 '12 at 07:46 AM

Seen: 527 times

Last Updated: Apr 05 '12 at 10:03 AM