InvokeRepeating(string, float, float)
Invokes the specified method after a specified delay, then repeatedly at the specified rate.
Read time 1 minuteLast updated 5 days ago
Definition
- Type: Method
- Namespace: UnityEngine
- Assembly: UnityEngine.CoreModule
public void InvokeRepeating(string methodName, float time, float repeatRate)
Parameters
Remarks
To cancel , use MonoBehaviour.CancelInvoke().
InvokeRepeatingThe and parameters depend on Time.timeScale. For example, a Time.timeScale of 2 effectively halves the real-time values of and , while a Time.timeScale of 0.5 doubles them. If Time.timeScale is 0, then the is never invoked.
timerepeatRatetimerepeatRatemethodYou can't change the value of the interval while is running. You must cancel and re-invoke to change it.
repeatRateInvokeRepeatingExamples
using UnityEngine;using System.Collections.Generic;// After an initial 2 second wait, launch a projectile every 0.3 secondspublic class ExampleScript : MonoBehaviour{ public Rigidbody projectile; void Start() { InvokeRepeating(nameof(LaunchProjectile), 2.0f, 0.3f); } void LaunchProjectile() { Rigidbody instance = Instantiate(projectile); instance.velocity = Random.insideUnitSphere * 5; }}