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.
Answer by Light997 · Feb 06, 2016 at 11:14 AM
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!
$$anonymous$$aybe... what if I don't want the delay as soon as the game starts? Isn't that what void Start is for?
I want to have the delay right at the point where I tell it to load the level which is inside the void Update.
I don't really understand how I can get the results I'm looking for using your example. The level load is the LAST thing that happens in the code and when you finish the race. I don't know why adding a delay seems so complicated for something so simple.
@TorQue$$anonymous$$oD Did you figure it out? I'm also looking to cause a delay after certain conditions are met within Update. Thanks!
Answer by MaliciousDiamonds · Sep 23, 2020 at 10:58 PM
@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 }
Your answer
![](https://koobas.hobune.stream/wayback/20220612063207im_/https://answers.unity.com/themes/thub/images/avi.jpg)