#How to wait

1 messages · Page 1 of 1 (latest)

foggy arch
#

@gentle veldt

#

What dont you understand about it specifically

gentle veldt
#

everything

#

im gonna learn how to do unity in general, get some knowledge of it, then go back to the issue

foggy arch
#
  1. You make a coroutine (code that runs alongside other code)

  2. You start that coroutine

  3. You wait in that coroutine, then you do what you want

gentle veldt
#

well i know how to do it

#

i just dont know how to implament it

foggy arch
#

uhh

gentle veldt
#

in my scenario

foggy arch
#

yield return new WaitForSeconds()
stuff you want to do?

gentle veldt
#

look at the movement code in my scratch game, thats what i want to make

foggy arch
#

you're lucky I've used scratch and know the language

#

tell any other programmer that and they'll tell you to fuck off lmao

gentle veldt
#

lol

#

so any ideas on what to do in that case?

#

i have the movement script all done, just need to add the wait

foggy arch
#

which part of the "movement" script do you want

#

oh

#

oh man there's a lot of problems with this but ill let it go since you're porting it from scratch

gentle veldt
#

i made it myself and it was the worst script ever

foggy arch
#

ok i'm assuming your WaitBeforeMove() coroutine is just a yield return new WaitForSeconds

#

which is a common mistake

#

the way you have it now, its useless

gentle veldt
#

so ivee been told

foggy arch
#

when you start a coroutine, its entirely seperate from the rest of that code in the method

#

you're literally just starting a timer and it doesnt do anything when its done

gentle veldt
#

i want to do
random code
wait 5 seconds
random code

foggy arch
#

so what you should do is

#
StartCoroutine(WaitStage());


IEnumerator WaitStage()
{
    yield return new WaitForSeconds(5);
    if (Random.Range(1, 20) < level)
    {
        if (area == "stage")
        {
            moved = true;
            timeset = 5;
            area = "diningroom";
        }
        else
        {
            timeset = 0.1f;
        }
    }
}
#

actually

gentle veldt
#

so id have to make a new one every time?

foggy arch
#

just one big coroutine

#

with all of the code

gentle veldt
#

ok then how do i run it in a forever loop

#

so to speak

foggy arch
#

put StartCoroutine() at the bottom

gentle veldt
#

which does what

foggy arch
#

StartCoroutine

gentle veldt
#

so id put that in the update method?

foggy arch
#

NO

#

Nononon

gentle veldt
#

ok i was hoping not

foggy arch
#

NEVER start a coroutine in Update

gentle veldt
#

ok

#

so where would i put it

foggy arch
#

unless ig you know its only going to start once

foggy arch
gentle veldt
#

so id make my big ienumerate and then at the end put StartCoroutine()?

foggy arch
#

yea

gentle veldt
#

alr

foggy arch
#

IEnumerator is a coroutine, if you havent figured that out

gentle veldt
#

so i put my big block of code in the ienumerate and then at the end before my } i put a StartCoroutine()

#

is that correct?

#

if so, alr, if not, explain
im headin to bed

gentle veldt
#

alr

gentle veldt
#

now how do i implament it so it triggers once per game and forever loop

foggy arch
#

as in once per start of a session or once per start of the main game loop

gentle veldt
#

uh whats the difference

#

like once per night attempt vs once per game?

#

@foggy arch

foggy arch
gentle veldt
#

ah

#

ok

foggy arch
#

if you want it to start when the script is loaded use Awake()