Hi,
I am trying to add a 2 sec wait after the destroy:
foreach (GameObject g in gemList1)
{
Destroy(g);
//want to add a time delay of 2 secs here
}
Invoke cant work because I have a parameter. Also IEnumerator seems to be prefixed before the function. Thanks for your ideas.
private IEnumerator DestroyGems()
{
WaitForSeconds wait = new WaitForSeconds( 2f ) ;
foreach (GameObject g in gemList1)
{
Destroy(g);
yield return wait ;
}
}
// …
StartCoroutine( DestroyGems() ) ;
###OR
private int gemIndex = 0 ;
private void DestroyNextGem()
{
Destroy( gemList1[gemIndex++] ) ;
if( gemIndex < gemList1.Length )
Invoke( "DestroyNextGem"; 2f ) ;
}
Very good answer by @Hellium - thought I’d add here a way of using it more than once
private int myIndex = 0;
[SerializeField] private float myDelay = 0.5f;
void MyFunction()
{
if (myIndex++ < myList.Count - 1)
{
myList[myIndex].FunctionCall();
Invoke("MyFunction", myDelay);
}
else
myIndex = 0;
}