I'm trying to solve what seems to be (or should be) a very simple problem. I'd like to sequence several steps in a GameManager class with some time delay between them. In my case, I'd like to clear the screen of objects, then ask my UIManager class to display a "Starting Level" message for 3 seconds, then spawn all the objects I need. This sequence should be repeatable unless a event that the GameManager subscribed to is triggered.

I'm trying to do this in the most generic, flexible and elegant way I can find. This is not a technical, "whatever works" question. I can do this with coroutines. I can do this with the GameManager calling an Action delegate to which UIManager is subscribed, then make the UIManager call an event that the GameManager is subscribed to when the displaying is done. But all these solutions seems convoluted to me.

I've started a thread on Unity Answers with some example code about this, which has generated a small discussion. In posting here I am hoping to find some new ideas, maybe generic to C# and not specific to Unity, on how to sequence a chain of methods. How would you achieve this?

To make this clearer, imagine a more complex sequence inside a GameSequence method:

  1. Initialize variables (level, difficulty etc).
  2. When done, play an introductory animation to that level, which can be of varying lengths depending on the level.
  3. Show a "Get ready" message for 3 seconds.
  4. Spawn the level and let it play.
  5. If the level ends successfully, animate some sort of cutscene (which can be of any duration).
  6. If the level doesn't end successfully, trigger a GameOverManager or something to that effect.

I hope I'm being clear in my question. Thank you for your help.

  • \$\begingroup\$ You already mention that your manager is subscribed to some events. What's the hindrance to generating an event after 3 seconds then? \$\endgroup\$ – Damon Jan 31 '18 at 16:10
  • 1
    \$\begingroup\$ It might seem like a stupid question, but how do you that, in Unity, without creating a coroutine just for waiting three seconds? I'm also interested in general principles for sequencing different phases of game, it's not just those three seconds :) \$\endgroup\$ – Cirrocumulus Jan 31 '18 at 16:20
  • \$\begingroup\$ Is Task.Delay not yet supported in Unity? Because that would be the canonical solution in an asynchronous C# program. Task.Delay is a coroutine that waits for some number of milliseconds, which is what you want. \$\endgroup\$ – Eric Lippert Aug 20 '18 at 20:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.