public void StartMatch(int levelId) { this.preset = LevelPresetData.GetPreset(levelId); CorouWaiter.Start(Routine()); IEnumerator Routine() { yield return(CorouWaiter.WaitFor(() => PlayerController.I != null)); yield return(CorouWaiter.WaitFor(() => UserHUDController.I != null)); PlayerController.I.Player.Health.OnDamage += (_, hp) => UserHUDController.I.SetHp(hp); PlayerController.I.Player.Health.OnDead += StopMatch; var waitSecond = new WaitForSeconds(1f); int counter = 3; while (counter-- > 0) { Debug.Log(counter + 1); yield return(waitSecond); } Debug.Log("Math started!"); var waitSpawn = new WaitForSeconds(this.preset.duration / (float)this.preset.count); counter = this.preset.count; while (counter-- > 0) { var enemy = spawner.SpawnEnemy(); enemy.OnCollide += Damage; void Damage(Enemy e, Collision c) { enemy.OnCollide -= Damage; var health = c.collider.GetComponentInParent <PlayerHealth>(); if (health != null) { health.SetDamage(1, enemy.GO); } } enemy.OnDeadByPlayer += Kill; void Kill(Enemy e) { enemy.OnDeadByPlayer -= Kill; ++this.killCount; } enemy.OnDestroyEvent += Destroy; void Destroy(Enemy e) { enemy.OnDestroyEvent -= Destroy; ++this.destroyCount; if (this.destroyCount == this.preset.count) { StopMatch(); } } yield return(waitSpawn); } } }