Address
304 North Cardinal St.
Dorchester Center, MA 02124
Work Hours
Monday to Friday: 7AM - 7PM
Weekend: 10AM - 5PM
We use cookies to ensure that we provide you with the best possible experience on our site.
Your essential source on Unity and web development
Your essential source on Unity and web development
Unity Invoke provides a means to execute a method within your class using the method name, with or without delay.
However, it is worth noting that while Invoke is a great tool, there are more elegant and controllable alternatives, such as coroutines.
This article explores how to use Invoke and examines its alternatives.
Table of contents
We can start with the classic usage of Invoke in Unity:
using UnityEngine;
public class InvokeMonoBehaviour : MonoBehaviour
{
private void Start()
{
// Invoke the "LaunchMissile" function after a 3-second delay
Invoke("LaunchMissile", 3f);
}
private void LaunchMissile()
{
// Actions to perform
}
}
In the code above, we invoke the “LaunchMissile” function after a 3-second delay.
Please note that if you set the delay to 0, Unity will execute the function at the end of the frame.
In this case, calling the function without Invoke may be more appropriate.
Another important detail is that even if you disable the GameObject after calling Invoke, Unity will execute the function until destruction of the object.
One drawback of the usage of Invoke is that it relies on a string representing the function name. If you ever rename the function through refactoring, this will result in an error. To address this, you can use “nameof“, starting from C# 6:
using UnityEngine;
public class InvokeMonoBehaviour : MonoBehaviour
{
private void Start()
{
// Invoke the "LaunchMissile" function after a 3-second delay using "nameof"
Invoke(nameof(LaunchMissile), 3f);
}
private void LaunchMissile()
{
// Actions to perform
Debug.Log("Missile launched");
}
}
To cancel an Invoke, you can use the CancelInvoke
method. Here is how you can do it:
private void Cancel()
{
// Cancel all Invokes in the MonoBehaviour
CancelInvoke();
// Cancel a specific Invoke for "LaunchMissile"
CancelInvoke(nameof(LaunchMissile));
}
Unfortunately, Invoke does not allow you to pass arguments.
To work with parameters, you will need to use coroutines.
Below is an example:
using System.Collections;
using UnityEngine;
public class InvokeCoroutine : MonoBehaviour
{
private void Start()
{
StartCoroutine(LaunchMissile(3f, 5));
}
private IEnumerator LaunchMissile(float delay, int number)
{
yield return new WaitForSeconds(delay);
// Perform your actions
}
}
Coroutines work differently than standard methods. Their functions have a return type of IEnumerator
and must include at least one yield
statement, which forces a pause in execution.
To invoke a coroutine, use StartCoroutine
. Even if not recommended, first parameter can still use the function name ornameof
.
To stop a coroutine, use the StopCoroutine
method. It is advisable to stop a coroutine using its reference rather than its name.
More or less the same way you used “nameof” with Invoke.
Here is an example:
using System.Collections;
using UnityEngine;
public class InvokeCoroutine : MonoBehaviour
{
private Coroutine _coroutineLaunchMissile;
private void Start()
{
// Store the reference in a property
_coroutineLaunchMissile = StartCoroutine(LaunchMissile(3f, 5));
}
private IEnumerator LaunchMissile(float delay, int number)
{
yield return new WaitForSeconds(delay);
// Perform your actions
}
private void StopCoroutineMissile()
{
// Stop all coroutines in the MonoBehaviour
StopAllCoroutines();
// Stop only one coroutine
StopCoroutine(_coroutineLaunchMissile);
}
}
According to Unity documentation, coroutines are faster than using Invoke.
Coroutines cease to execute when a GameObject is deactivated or destroyed, while Invoke continues to run as long as the object exists.
Coroutines can be chained and offer a variety of yield
statements to address various use cases:
WaitUntil
: Pauses execution until a specified function or delegate returns true
.WaitWhile
: The opposite of WaitUntil
.WaitForSeconds
: Delays for a set number of seconds.WaitForSecondsRealtime
: Delays using unscaled time.WaitForEndOfFrame
: Delays until just before the frame finishes rendering.WaitForFixedUpdate
: Useful for physics-related work.For a comprehensive understanding of coroutines, please refer to Unity documentation.
In conclusion, while Invoke is a functional method for invoking functions in Unity, coroutines offer better performance, control, and a more robust approach for handling complex scenarios.
Consider adopting coroutines for your Unity projects to enhance efficiency.