Are you using this just as an example of time consuming task, or do you actually need to wait a set time? if you actually need to wait a set time there is yield return new WaitForSeconds(n) in the IENumerator (coroutine) that will wait for n seconds. [Docs on WaitforSeconds][1]
Multi threaded method
That said you could also use a new Thread for this, depending on what you want to do. When working on multiple threads in unity keep in mind that you will not be able to access monobehaviour from the new thread. meaning things suchs as gameObject.transform.position etc will not be accessible. An exception to this is Debug.Log which can be accessed. use a coroutine if you want to edit a gameObject instead.
Starting a new thread itself is quite easily done like so, a pro of doing this is that you can use Thread.Sleep if you want to wait for a set time without losing any performance which you still lose on a coroutine:
using System.Threading; public class ThreadStarter { Start() { Debug.Log("a"); Thread newThread = new Thread(() => FunctionRunningOnThread());//this function will now execute on the new thread newThread.Start(); Debug.Log("c"); } private void FunctionRunningOnThread() { Thread.Sleep(5000)//Thread.Sleep is in milliseconds, so this waits 5 seconds Debug.Log("B"); //debug.log } }
this will print "a c" and then "b" after 5 seconds. Keep in mind though that working safely with multiple threads is harder than working on a single thread as you'll have to deal with race conditions, locking variables so you don't try to edit them from multiple places at the same time and way more stuff. It is discouraged to do unless you know what you're doing, and using coroutines is the safer option.
Starting a coroutine A coroutine function always needs to be of type IENumerator and is called using the StartCoroutine function and always returns a value using yield return this value can be null
private void Start() { Debug.Log("a"); StartCoroutine(SomeFunction()); Debug.Log("c"); } private IEnumerator SomeFunction() { yield return new WaitForSeconds(5);//this will wait 5 seconds Debug.Log("b"); //Alternatively you can use this if you want to wait a set of frames for (int i = 0; i < 50000; i++)//this will wait 50000 frames { yield return new WaitForEndOfFrame(); } }
This too will print "a" "c" and then "b" after 5 seconds. the pro of this is you can still edit GameObjects inside the coroutine, and you don't have to bother about thread safety. [1]: https://docs.unity3d.com/ScriptReference/WaitForSeconds.html