Invoke method after time gap easy way Unity 3d | Invoke and coroutines in unity
In this video I have shown a little description of how we can invoke anything like a method after time gap using
* Invoke method
* Coroutine and IEnumerator
Code :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/////////// thank you ////////////
public class TimegapExample : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
//Invoke (nameof(WaitThenprint),2.0f);
//or Invoke(WaitThenprint,2.0f);
////////////// for passing parameters we use coroutine and ienumerators ////////////////
StartCoroutine(WaitThenPrint(2.0f,"ht"));
}
///////////// this method simply prints "k"
void WaitThenprint(){
print("k");
}
////////////// this will print the passed string after time gap which is also specified in parameter/////////
IEnumerator WaitThenPrint(float timegap,string message){
yield return new WaitForSeconds(timegap);
print(message);
}
}