Add points every five seconds
I want to give a Player an Increment in Pints about 5 points every 5 secounds but the function just wait 5 seconds and then start adding Points every frame I need some solution Please, and thanks by advanced
void Update()
{
StartCoroutine("AddPoints");
}
void Points()
{
this.gameObject.GetComponent<MAIN>().Points += 5;
}
IEnumerator AddPoints()
{
yield return new WaitForSeconds(5f);
Points();
}
Answer by tanoshimi · Jan 20, 2017 at 09:11 AM
That's because you're launching a new instance of the coroutine every frame in Update.
You could either do:
void Start()
{
StartCoroutine("AddPoints");
}
void Points()
{
this.gameObject.GetComponent<MAIN>().Points += 5;
}
IEnumerator AddPoints()
{
while(true) {
yield return new WaitForSeconds(5f);
Points();
}
}
Or, more simply:
void Start()
{
InvokeRepeating("Points", 0f, 5f);
}
void Points()
{
this.gameObject.GetComponent<MAIN>().Points += 5;
}
The problem is that I will only add points of the player is in a safe zone so I need to use uptade to check if the player is in there. If I use start and I stop the buckle I guess i wont be able to execute again
Your answer
Follow this Question
Related Questions
Coroutine not working on android build 1 Answer
Coroutine cancels for no reason 0 Answers
Coroutine, while, and Waitforseconds 1 Answer
StopCoroutine() Not Working 2 Answers