x


Programming WaitForSeconds

Just out of curiosity (I'm learning yield and unity scripting), would it be possible to implement WaitForSeconds in Javascript? How would you do it?

What about C#?

EDIT: Ok, it seems I didn't express myself correctly. My question is hypothetical, let's say you didn't have the WaitForSeconds class available and you needed to create it from scratch. How would you program a class that had the same behavior as the class provided by Unity?

more ▼

asked Mar 02 '10 at 05:05 PM

Ezequiel gravatar image

Ezequiel
75 4 6 13

I'd suggest reading the documentation, which uses Javascript for all the code examples, and C# where there are significant differences.

Mar 02 '10 at 09:16 PM Eric5h5

I think I've been misunderstood. I'm not asking how to USE WaitForSeconds. I'm asking: If you didn't have WaitForSeconds already implemented, how would you program it from scratch?

Mar 02 '10 at 11:46 PM Ezequiel
(comments are locked)
10|3000 characters needed characters left

3 answers: sort voted first

In Javascript:

function Start () {
    yield MyWaitFunction (1.0);
    print ("1");
    yield MyWaitFunction (2.0);
    print ("2");
}

function MyWaitFunction (delay : float) {
    var timer = Time.time + delay;
    while (Time.time < timer) {
        yield;
    }
}

In C#:

using UnityEngine; 
using System.Collections; 

public class TimerTest : MonoBehaviour { 
    IEnumerator Start () {
        yield return StartCoroutine(MyWaitFunction (1.0f));
        print ("1");
        yield return StartCoroutine(MyWaitFunction (2.0f));
        print ("2");
    }

    IEnumerator MyWaitFunction (float delay) {
        float timer = Time.time + delay;
        while (Time.time < timer) {
            yield return null;
        }
    }
}

Alternately (Javascript):

function MyWaitFunction (delay : float) {
    var t = 0.0;
    while (t < delay) {
        t += Time.deltaTime;
        yield;
    }
}
more ▼

answered Mar 03 '10 at 12:54 AM

Eric5h5 gravatar image

Eric5h5
80.3k 42 132 521

@ Eruc5h5

Thanks brother it's perfect Solution,

I am facing issue with WaitForSecond when Time.timeScale set to 0, i tried lot of same kind solution but all are incomplete... I have invested more-then 6 developing hour to find the solution every time I got Back with function when i tried to made it reusable as you made, cos every time I use it without StartCorroutine

With solution you provided I feel relax now and invest my developing hour in real world developing . Thanks Again brother

Jan 05 at 11:40 AM PAHeartBeat
(comments are locked)
10|3000 characters needed characters left

javascript

function Start ()
{
Debug.Log ("start time");
yield WaitForSeconds(2);
Debug.Log ("two seconds later");
}

in C# you should add System.Collections to your namespaces. also you define a coroutine by returning an IEnumerator.

IEnumerator Start ()
{
print ("first");
yield return new WaitForSeconds(2f);
print ("2 seconds later");
}

2f or 2.5f means that the number is a float and not a double or int. you should return an instance of WaitForSecond in C#. see this pages for more info. http://unity3d.com/support/documentation/ScriptReference/index.Writing_Scripts_in_Csharp.html http://unity3d.com/support/documentation/ScriptReference/index.Coroutines_26_Yield.html

more ▼

answered Mar 02 '10 at 08:46 PM

Ashkan_gc gravatar image

Ashkan_gc
9.1k 33 56 117

My question is not how to use it. My question is an hypothetical one: Let's say you didn't have WaitForSeconds already available, how would you implement the same function? i.e. How would you create the class WaitForSeconds yourself?

Mar 02 '10 at 11:49 PM Ezequiel
(comments are locked)
10|3000 characters needed characters left

just put this in the function where ever you need it to stop for a while:

yield WaitForSeconds(4); //4 being the number of seconds you want
more ▼

answered Mar 02 '10 at 08:37 PM

Adam Bruns gravatar image

Adam Bruns
274 24 27 38

My question is not how to use it. My question is an hypothetical one: Let's say you didn't have WaitForSeconds already available, how would you implement the same function? i.e. How would you create the class WaitForSeconds yourself?

Mar 02 '10 at 11:49 PM Ezequiel
(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:

x5097
x336
x108

asked: Mar 02 '10 at 05:05 PM

Seen: 21966 times

Last Updated: Jan 05 at 11:40 AM