Invoke
Tutorial
·
Beginner
·
+10XP
·
5 mins
·
(1717)
Unity Technologies

The Invoke functions allow you to schedule method calls to occur at a later time. In this video you will learn how to use the Invoke, InvokeRepeating, and CancelInvoke functions in your Unity scripts.
This tutorial is included in the Beginner Scripting project.
Previous: Arrays
Next: Enumerations
Languages available:
1. Invoke
InvokeScript
using UnityEngine;
using System.Collections;
public class InvokeScript : MonoBehaviour
{
public GameObject target;
void Start()
{
Invoke ("SpawnObject", 2);
}
void SpawnObject()
{
Instantiate(target, new Vector3(0, 2, 0), Quaternion.identity);
}
}
InvokeRepeating
using UnityEngine;
using System.Collections;
public class InvokeRepeating : MonoBehaviour
{
public GameObject target;
void Start()
{
InvokeRepeating("SpawnObject", 2, 1);
}
void SpawnObject()
{
float x = Random.Range(-2.0f, 2.0f);
float z = Random.Range(-2.0f, 2.0f);
Instantiate(target, new Vector3(x, 2, z), Quaternion.identity);
}
}