Reputation: 11
I'm developing a small game currently that involves players guessing. The player can additionally use special cards, that, for example, reveal one letter/add time he/she has for guessing and so on. Game is divided into two scenes -> one with shop, inventory, player profile etc the second one is strictly for guessing. Currently, almost everything runs in the update in the second scene, but I really hate it. I was trying to rewrite everything into coroutines BUT the problem is that it seems impossible to use cards inside IEnumerator (or maybe I'm doing something wrong?). For example, a simple countdown. If it's in Update I can easily influence the time with using cards(f.e. add 30 seconds). In the case of IEnumetor, I can't (Or maybe better, I don't know how to do it).
int secondsForGuess = 30;
IEnumerator Countdown () {
int counter = secondsForGuess;
while (counter > 0) {
yield return new WaitForSeconds (1);
counter--;
}
}
Any general suggestion how to do it without using Update will be greatly appreciated :D
Upvotes: 0
Views: 280
Reputation: 90629
If I understand your question right, you want to be able to increase the counter from the outside of the routine. So simply make it a field in the class so anyone can increase or decrease it:
const int secondsForGuess = 30;
private int counter;
public void AddToCounter(int seconds)
{
counter += seconds;
}
private IEnumerator Countdown ()
{
counter = secondsForGuess;
while (counter > 0) {
yield return new WaitForSeconds (1);
counter--;
}
// Do something when finsihed
}
Also just in case: Make sure to somewhere start the routine using StartCoroutine
.
Upvotes: 1