Hello,
I’m a beginner at Unity, I’ve done many tutorials and since some weeks I’m on a project.
So there is my problem :
I want to use a timer in a class, so I made a class in the namespace tools which contains a Coroutine to create and start a timer.
I realized that I need to declare a Timer Object in my class, but I’ll use 2 timers at the same time so there is my question on this code :
void method1( float seconds )
{
timer.CreateTimer(2.0f);
}
void method2( float seconds )
{
timer.CreateTimer(2.0f);
}
public void call ( float seconds1, float seconds2 )
{
method1(seconds1);
method2(seconds2);
}
The method2 will start when method1 is ended ?
The answer is no, both will start and end at the same time, unless your timer class internally does the actual staying of Coroutines one after the other.
Method2 will be called after method1 ends. But the latter ends when you create the Timer, not when the Timer ends. To do this kind of things with Coroutine I usually use callbacks. For example:
void Start() {
StartCoroutine(Timer(2, DoSomething))
}
//The Coroutine
IEnumerator Timer(float time, callback) {
float timer = 0;
while (timer < time) {
timer += Time.deltaTime;
yield return null;
}
callback();
}
void DoSomething() {
//Do something
}
Note: When you pass the method to the Coroutine you don’t call it (you don’t put the parenthesis). You just put the name and it will be called inside the coroutine. Hope it helps 
As @hexagonius pointed out, no. All your code in the same execution flow that is not in a Coroutine will be executed instantly in order and in the same frame.
So if you want to do waiting, you never get around Starting a new coroutine for that. You can either pass the code you want to execute after waiting to the Timer() coroutine, as @Matt1000 pointed out, or you start a new coroutine that waits for the Timer()s to finish before starting the next one. I like the second method more, because it makes the flow in the code easier to read.
In a coroutine, you can yield other coroutines. The coroutine then waits of the other coroutine to be finished at this point. Just make sure that your methods that create coroutines return them like so:
public Coroutine Timer(float duration)
{
return StartCoroutine(WaitForSeconds(duration));
}
private IEnumerator WaitForSeconds(float duration)
{
yield return new WaitForSeconds(duration);
}
Then you should be able to chain the coroutines in another coroutine like so:
public Coroutine Call ( float seconds1, float seconds2 )
{
return StartCoroutine(ExecuteCall(seconds1, seconds2));
}
private IEnumerator ExecuteCall(float seconds1, float seconds2)
{
yield return Timer(seconds1);
//code to execute after first timer finished here
yield return Timer(seconds2);
//code to execute after second timer finished there
}
However, at this point, Timer is nothing more than an alias for WaitForSeconds.