Delay without coroutine

In my script I need a delay, but I can’t use the coroutine. I am a little confused with this problem, so I can’t describe, why I can’t use coroutine. I just need a delay between case 2 and case 3.

void Update() 
{
    if (Input.GetKeyDown(KeyCode.P))
        shouldIncreaseLadder = true;

    if (shouldIncreaseLadder)
    {
        switch(increaseLadderCallNumber)
        {
            case 0: IncreaseLadderGroupSize(ladderGroup0); break;
            case 1: IncreaseLadderGroupSize(ladderGroup1); break;
            case 2: IncreaseLadderGroupSize(ladderGroup2); break;

            // I need to call this function after delay in 2 seconds
            case 3: IncreaseLadderGroupSize(ladderGroup10); break;
            case 4: IncreaseLadderGroupSize(ladderGroup11); break;
            default: break;
        }            
    }            
}

void IncreaseLadderGroupSize(GameObject[] ladderGroupArray)
{
    foreach (GameObject i in ladderGroupArray)
    {
        //.. some code ..
        shouldIncreaseLadder = false;

    }
    shouldIncreaseLadder = true;
    isLadderGroupDone = false;
}

Set up a timer variable…

float timer = -1;  // -1 means don't do anything with it yet

After your case 2 runs, set the timer to 0…

case 2: your_stuff();  timer = 0;  break;

Just inside the shouldIncreaesLadder if statement…

if (shouldIncreaseLadder)
{
   if (timer >= 0)
   {
     timer += Time.deltaTime;
     if (timer >= TheAmountOfTimeToWait)
     {
       // turn off the timer
       timer = -1;
     }
     else
     {
       // don't run the rest of the code - i.e. return from Update
       // so your case statement doesn't run
       return;
     }
   }

   // your case statement goes here
}

That’s kind of the basic idea - increment a value by frame time until it reaches your threshold - don’t run the code that needs to wait until your timer reaches the threshold.

This kind of thing gets a little hacky, so you’d want to probably encapsulate the timer stuff and clean things up.

I know you don’t want to say, but why can’t you use a Coroutine? They were designed for cross-frame logic like this.