How to get function to run every x seconds and then every y seconds?

I have a coroutine that I want to run every 10 seconds, and then every 15 seconds. So that would look like coroutine runs for the first time,

10 seconds later it runs for the second time,

15 seconds later it runs for the third time,

10 seconds later it runs for the fourth time,

I’ve tried two parallel InvokeRepeatings at an offset with different delays, but one will always be called at least twice before the other one.
I’ve tried:

private IEnumerator Timing()
{
    yield return new WaitForSeconds(delays[state % 2]);
    state++;
    ChangeState();
}

but I don’t know how to get this running continuously without messing up the coroutine that this one calls.
I’m also trying to get this as efficient as possible because there will be many objects with this script.
Any help would be appreciated, even if you can just point me in the right direction.

What I would do in that situation is have a bool (e.g. “oddRun”/“evenRun”) and have an if else statement. If it oddRun = true, yield return new WaitForSeconds(10);, if oddRun = false, yield return new WaitForSeconds(15); You will want to put the logic of your coroutine before this (see my example below). Also you need to use StartCoroutine(CoroutineName()) you can’t just call a coroutine by its name.

bool oddRun = true;
private IEnumerator Coroutine_you_want_to_run()
 {
    (Put the logic you want to run in the coroutine here)
    (Can put a Debug.Log("Test") to test whether the timing is working)

    if (oddRun) {
        oddRun = false;
        yield return new WaitForSeconds(10);
    }
    else {
        oddRun = true;
        yield return new WaitForSeconds(15);
    }
     
     StartCoroutine(Coroutine_you_want_to_run());
 }

If this worked, please mark it as answered. If you still have questions, feel free to ask I’m happy to help :slight_smile:

something like that?

	IEnumerator YourCoroutine(){
		while(true){
			// code here
			yield return new WaitForSeconds(10f);
			// code here
			yield return new WaitForSeconds(15f);
		}
	}