x


How to delay a function (how to use WaitForSeconds() and yield)

I know this has been asked before, but it just doesn't work, and I don't see what I'm dooing wrong.

(I use javascript)

function Quit (score:int) //quits to main menu (scene)
{
    if (currentLevel>0) scores[currentLevel-1] = Mathf.Max(scores[currentLevel-1],score);
    currentLevel=0;
    //Application.LoadLevel(currentLevel);
    LoadCurrentLevel();
}

function LoadCurrentLevel() //is een coroutine (blijkbaar automatisch ofzo doordat er yield instaat ?
{
    yield WaitForSeconds (2);
    Application.LoadLevel(currentLevel);
}

I've read the documentation and I thought this would work, but it seems to wait for ever (instead of 2 seconds), Application.LoadLevel(currentLevel) is never executed.

don't know if it could be relevant, but this code is not part of a script attached to a gameobject (and it's a singleton).

EDIT:

I tried it differently now, like this:

function LoadCurrentLevel(wait:boolean) 
    {
        //if (wait) yield WaitForSeconds (2.5);
        //Application.LoadLevel(currentLevel);
        if (wait) 
        {
            isWaiting=true;
            waitingStart=Time.time;
        }
        else    Application.LoadLevel(currentLevel);
    }

    function Update()
    {
        if (isWaiting)
        {
            if (Time.time>waitingStart+2.5)
            {
                isWaiting=false;
                Application.LoadLevel(currentLevel);
            }
        }
    }

But it still doesn't work, it does exactly the same. why is that ?

more ▼

asked Dec 29 '10 at 12:23 PM

Steven 1 gravatar image

Steven 1
377 28 36 49

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

1 answer: sort voted first

Your script is correct. You need to attach your script to a GameObject.

What I do is create an Empty GameObject for all my games, and call it 'GameManager'.

Any game-wide scripts are then attached to that game object.

more ▼

answered Dec 29 '10 at 12:28 PM

Meltdown gravatar image

Meltdown
5.6k 18 25 49

is there a way without attaching it to a GameObject ? it's just a lot cleaner without

(also, I would have to rewrite all the singleton stuff, so it works with the gameobject and it doesn't get deleted when switching levels ...)

Dec 29 '10 at 12:39 PM Steven 1

tried a different approach, see edit

Dec 29 '10 at 12:57 PM Steven 1

As far as I know it needs to be attached to a GameObject. There is nothing messy about having a GameObject to manage all your global game logic. I find it very clean and efficient.

Dec 29 '10 at 01:08 PM Meltdown

ok but I still don't get at all why my 2nd approach doesn't work. (the messy thing is that I have to create a gameobject at the beginning of the game and make sure it never gets deleted and is accessable from everywhere)

Dec 29 '10 at 01:15 PM Steven 1

DontDestroyOnLoad(this.gameObject); will make any object a script is attached to be a perm. resident of your game world even on scene changes. The easiest approach is to create one at the beginning map, tag it with a unique tag to retrieve the object easily and then use its script.

Apr 27 '11 at 05:25 PM Bryan 4
(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:

x570
x333
x179
x169

asked: Dec 29 '10 at 12:23 PM

Seen: 3342 times

Last Updated: Dec 29 '10 at 12:51 PM