I want to have cancelinvoke start multiple seconds arfter object becomes invisible

I have a shooting game and I want to have the enemy stop shooting multiple seconds after becoming invisible so maybe creating like a in game timer and if >5 cancel invoke and restart on visible. In simpler term I want to wait for x number of seconds before cancelling invoke.

void OnBecameVisible(){
InvokeRepeating (“CreateBullets”, ShootTime, ShootTime);
ShootTime = T.Next(2,4);
}
void OnBecameInvisible(){
CancelInvoke (“CreateBullets”);
}

1 Answer

1

You need to use Co-routine. It has wait for seconds function.

Link:Unity - Scripting API: Coroutine