How to create a timer. [Redone Post]

Right, I had to retype this post, because I am not sure I have explained my problem clearly.
I am wanting to create a unique timer class I can refer to pause the calling script at the current line then resume after the desired time has eclasped.

The below is how im wanting to call it from another script with the formating.

Public void aScriptThatNeedsATimer()
{
    //Blah blah script stuff etc.
    Timer(5); //Waits 5 seconds before starting next line.
    //More blah blah script stuff etc.
}

I have been informed that Invoke and Couroutines would be best, but im unsure how to implement these in the current context.

if that is the only thing in the Update, then you can get better performance using coroutines or Invoke from Start/OnEnable or public method.

what is wrong with the code you have ?

Use a coroutine and WaitForSeconds.

Start can actually be an auto coroutine:

IEnumerator Start()
{
    yield return new WaitForSeconds(5f);
    SomethingEpic();
}

Otherwise as a normal Coroutine:

void Start()
{
    StartCoroutine(MyRoutine());
}

IEnumerator MyRoutine()
{
    yield return new WaitForSeconds(5f);
    SomethingEpic();
}

Or if you’re using the newer .net framework, you can check out the async stuff and await a Delay of 5 seconds.

async void Start()
{
    await Task.Delay(TimeSpan.FromSeconds(5f));
    SomethingEpic();
}

Or Invoke/InvokeRepeating. There are pros and cons to these versus coroutines, but in most cases these probably aren’t an issue.

One difference is that a coroutine essentially sets a marker in your code that will be returned to after N seconds. This means you can put literally anything after it to be executed later. Using Invoke is more restrictive as it only allows you to call a specific function, and you can’t (can you?) pass any arguments to it. However, I’ve never had to pass any, because those functions are typically some kind of check (using global variables), an Instantiate (which just “does its thing” locally), etc.

Erm…sorry…could you expand on this please?
Ive looked into Invokeing but someone mentioned its not memory efficent.
Im interested in the normal and auto Coroutines.
My question is, how would these be called by another script?

Instead of putting StartCoroutine in the Start method, it is placed in a public method. Other script has to have knowledge of someclass. Here are some ways that scripts get knowledge of each other.

//you can use one of these methods to grab the someclass
someclass someinstance = gameobject.Find(“path”).getComponent();

//works if you have only one singleton instance of monobehavior someclass.
someclass someinstance = FindObjectByType();

//works if you have multiple objects containing someclass.
someclass[ ] someinstances = FindObjectsByType();

  • public class someclass {
  • public void SomePublicMethod()
  • {
  • StartCoroutine(MyRoutine());
  • }
    • IEnumerator MyRoutine()
  • {
  • yield return new WaitForSeconds(5f);
  • SomethingEpic();
  • }
    • }