trouble setting waiting time in my code

Hi y’all,

so I want my code to wait for a certain amount of time and I can’t figure out how…

here’s a part of my script, and it doesn’t work…

void Update()
   {
        else if (setSpeed > shipSpeed.curentSpeed)
        {
            shipSpeed.curentSpeed += acceleration;
            StartCoroutine(Wait());

        }
        else if (setSpeed < shipSpeed.curentSpeed)
        {
            shipSpeed.curentSpeed -= acceleration;
            StartCoroutine(Wait());

        }
   }

        IEnumerator Wait()
        {
            Debug.Log("waiting 5sec...");
            yield return new WaitForSeconds(5f);
        }

Coroutines do not pause code like that. You would have to put the code in the coroutine, and loop within the coroutine.

IEnumerator Wait()
{
   while(someBoolThatIsTrue)
{
   //if Statement
  yield return new WaitForSeconds(5f);
}
}

Example, the code above enters the while loop, runs the code once, then waits 5 seconds before it loops.

In your code, it runs the if statement, starts the coroutine which then hits the 5 seconds mark, returns to the Update and runs the rest of the code in Update before looping again to run Update again. It does not pause your code in Update. There are also ways to have it skip code in Update using your own timer and some prefer that, but otherwise, just run all the Update code in your coroutine with the yield in there and it will be fine.

ok thanks for that. with that I might be able to make it work. I’ll keep you updated.

Still doesn’t work…

-Created a bool method to check if my condition is true or false.

-I set the while loop inside the IEnumerator with the bool methode

-I started the IEnumerator coroutine inside update.

Result: all the code inside the coroutine work execpt the waiting time

Am I supose to use a special namespace or something?

What exactly is your goal here?

HEY! I remember that cat!!! :smile:

my goal is to set the value of a float variable let’s say 100 and have an other float variable geting to that value by y increment every x second.

already saw on one of my course how to do it but since I never used it I can’t remember…

Do you want the value to change smoothly, or in discrete chunks?

by chunk of let say 5 for now (it’s an other variable the value of the chunk will change)

I also tried to use InvokeReapeting(“Method”,5,5), but onece the if statement are stated ther is no 5 secon delay between each increment…

Try posting your updated code, that helps us to help you.

here it is:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

void Update()
    {
        ShipControle();
        InvokeRepeating("ShipAcceleration1", 5, 5);
    }

public void ShipAcceleration1()
    {
        if (setSpeed - shipSpeed.curentSpeed < 0 && setSpeed - shipSpeed.curentSpeed > -acceleration)
        {
            shipSpeed.curentSpeed = setSpeed;
        }

        else if (setSpeed - shipSpeed.curentSpeed > 0 && setSpeed - shipSpeed.curentSpeed < acceleration)
        {
            shipSpeed.curentSpeed = setSpeed;
        }

        else if (setSpeed > shipSpeed.curentSpeed)
        {
            shipSpeed.curentSpeed += acceleration;        
        }

        else if (setSpeed < shipSpeed.curentSpeed)
        {
            shipSpeed.curentSpeed -= acceleration;
        }
       
    }

With that it take 5 second before the first change and then it happen multiple time a second.

@Brathnann I did try to but the if statement inside the coroutine, but it did work. no waiting time at all.

Part of the issue is you are calling InvokeRepeating from Update. What is happening is it calls it every single frame which means it continues to build up calls over and over. You now have 100s or 1000s of InvokeRepeating calls building up, so it does technically wait 5 seconds, but if you got 60 fps for example, you now have 60 calls per second, all in a nice queue. And it just keeps stacking.

And I wanted to see your coroutine call because now after seeing this, I suspect you had the same issue with your coroutine. You probably called StartCoroutine in update over and over and over again, creating several looping coroutines.

You need to do the calls once in Start and only call it again if you stop it for some reason but need to restart it.

So the first one will work fine, but after all the other calls just execute in a line.

yeah I did call it in the update…

question: if I call the coroutine in the start method with a while loop inside the coroutine like you sais, when I turn the bool variable on and off, do I need to call the coroutine again?

alright I have the efect I whant with the invoke repeating in the star. no need for a coroutine. THANKS for the help!!!

Yes, if you set the bool to false, the coroutine would exit the while loop and end, thus you’d have to call it to restart it. Or leave it looping in a condition where it doesn’t run any code for a bit.

I’m also not a fan of InvokeRepeating due to the lack of compile time checking, but glad you got your code working.

still new to coding what do you mean by “lack of compile”?

As you can see, InvokeRepeating takes a string. Because it’s just a string, when you return to Unity and it recompiles your code, it doesn’t check if that method actually exist. So right now you are safe, but what happens if you change the method name. Then you return to the game, hit play and suddenly it’s not working right and maybe you get lucky and figure it out, but maybe you struggle for hours getting stuck. Or, if you ever work with another developer who changes the name and suddenly they don’t know what is going on.

Now you might think you’ll name do either of those, but it does happen and I’ve seen that mistake several times with newer programmers at my work.

If you use the generic version of the coroutine when you start the coroutine (don’t use the string version as this runs into the same issues), it will act just like you trying to call a method and throw an error as soon as you return to Unity if you didn’t see the error pop up in your script editor software that the method doesn’t exist.

That’s the basic idea of it.

oh! I see cool thanks and yea in that regard it’s way better.

my problem with the coroutine is once it turned of I can figure a way to restart it when I need it without puting it in update and that’s a no go since I come back to my early problem.

for now I think i’ll stick to invok repeating in the start method