C# Simple delay at void Update?

Ok guys, apologies for my ignorance but I’ve come to Unity from UE4 and working with blueprints so my programming knowledge is seriously lacking. In UE4 if I want to pause the code before it executes an action, I just thrown in a simple delay node, give it a time to delay (4 seconds) and then route that into my action (Load Level).

From every search I’ve done they all say in order to delay an action in Unity, you need to create a coroutine in the void start and execute the action there. Problem is in my code (followed a tutorial) I’m changing the level after crossing a checkpoint (racing game) which is executed in the void Update section of the code. I’ve tried using a coroutine but I don’t understand what I’m doing well enough so I’m sure I’m implementing it incorrectly. Regardless I can’t get the code I want working so I’m asking for help here.

My Code:

using UnityEngine;
using System.Collections;

public class GameState : MonoBehaviour 
{
	public AudioSource raceCompletedSound;
	public float raceStartTimer				= 0.0f;
	public float resetRaceStartTimer		= 0.0f;

	public int totalLaps					= 3;

	public GameObject playerCraft;
	public GameObject[] enemyCrafts;

	public string raceCompletedText;
	public GUISkin CurrumGUISkin = null;

	private bool raceCompleted = false;


	// Use this for initialization
	void Start () 
	{
		resetRaceStartTimer = raceStartTimer;
		playerCraft = GameObject.FindGameObjectWithTag ("Player");
		enemyCrafts = GameObject.FindGameObjectsWithTag ("Enemy");
		HoverControl hoverControl = playerCraft.GetComponent<HoverControl> ();
		MovementEngine movementControl = playerCraft.GetComponent<MovementEngine> ();
		hoverControl.enabled = false;
		movementControl.Thrust = 0;
	}

	void OnGUI() 
	{
		GUI.skin = CurrumGUISkin;

		if (raceCompleted == true) 
		{
			GUI.Label (new Rect (Screen.width/2.5f, Screen.height/2.5f, Screen.width, Screen.height), raceCompletedText);
			raceCompletedSound.Play();
		}
	}

	// Update is called once per frame
	void Update () 
	{
		HoverControl hoverControl = playerCraft.GetComponent<HoverControl> ();

		raceStartTimer -= Time.deltaTime;
		if (raceStartTimer <= 0.0f) 
		{
			hoverControl.enabled = true;
			EnemyMovement.raceStarted = true;
			raceStartTimer = 0.0f;
		}

		if (CheckpointController.currentLap == totalLaps + 1) 
		{
			EnemyMovement.raceStarted = false;
			hoverControl.enabled =false;
			raceCompleted = true;
			MovementEngine movementControl = playerCraft.GetComponent<MovementEngine> ();
			HoverOrientation movementTorque = playerCraft.GetComponent<HoverOrientation> ();
			movementControl.Thrust = Mathf.Lerp (1f, 0f, 1f);
			movementTorque.MaxTorque = Mathf.Lerp (1f, 0f, 2f); 

			//Figure out code to delay the level load

			//End Figure out code to delay the level load

			Application.LoadLevel ("LevelSelect");


		}
	}

}

I’ve tried implementing a coroutine by reading the Unity Manual but even when I add the exact code they suggest (obviously I realize this is fading a color but just for testing)

IEnumerator Fade() {
    for (float f = 1f; f >= 0; f -= 0.1f) {
        Color c = renderer.material.color;
        c.a = f;
        renderer.material.color = c;
        yield return null;
    }
}

MonoDevelop highlights the ( immediately following the Function name “Fade” in red and Unity claims its an error “Expecting something else”.

Essentially what I want to do is pause the code for 5 seconds before loading the “LevelSelect” level because with my current code, the level loads the second you cross the finish line.
Any idea how would I go about doing this?

Thank you kindly.

Turns out you don’t even need Coroutines for this!

Don’t worry, I didn’t know about this first too, but when I found out about it, it blew my mind!

The Invoke() function lets you call functions with a certain delay. The syntax goes like this:

void Start()
{
     //Calls the function after 5 seconds
     Invoke(MyFunction, 5.0f);
}

void MyFunction()
{
     //Do whatever you want to do after the set amount of time here
}

Let me know if my answer helped!

@TorQueMoD I found a fix to the problem if you still need it:

` void Start()
{
//Calls the function after 5 seconds
Invoke(“MyFunction”, 5.0f);
}

void MyFunction()
{
//Do whatever you want to do after the set amount of time here
}`