How do I get a "counter" to reset back to "0"
I have a gameobject projectile "grenade" in my game that gets thrown into a Bowl and destroys all the balls in the bowl. I would like this grenade to also set the counter back to "0" since all the balls are out of the bowl. I've tried adding OnCollisionEnter () into the script, I've also tried creating a new function but nothing seems to reset the counter back to "0" after the grenade goes off. Here's the code: public class ballcounter : MonoBehaviour {
public int totalballs =0;
private GameObject bola;
private GameObject[] allballs;
private int i = 0;
public void CountBalls(GameObject bola)
{
totalballs++;
allballs[i] = bola;
i++;
}
private void Start()
{
allballs = new GameObject[100];
StartCoroutine(Destroy());
}
IEnumerator Destroy()
{
while (true) {
yield return new WaitForSeconds (60f);
if (totalballs > 6)
{
Time.timeScale = 0;
}
else if (totalballs <= 6 && totalballs > 0)
{
for (int j = 0; j < allballs.Length; j++)
{
if (allballs [j] != null)
{
Destroy (allballs [j].gameObject);
}
}
i = 0;
totalballs = 0;
}
}
}
}
Answer by Obsessi0n · Feb 02, 2018 at 08:08 PM
Try adding this to the ballcounter
public void ClearBucket()
{
StopCoroutine(Destroy());
for (int j = 0; j < allballs.Length; j++)
{
if (allballs[j] != null)
{
Destroy(allballs[j].gameObject);
}
}
totalballs = 0;
StartCoroutine(Destroy());
}
Then when the grenade explodes make it call the function "ClearBucket()" the function will delete all balls and set the totalballs to 0 also will restart the coroutine that counts the time.
Fixed the code had StopCoroutine ins$$anonymous$$d of StartCoroutine.
The thing is, I don't want the grenade to restart the Coroutine. As the Coroutine is counting down, I need to be able to reset the ball counter to "0" with the grenade. Essentially the grenade is a "special weapon" that if thrown in the bowl in time, can put the ball counter back to "0" before the Coroutine reads that there are 8 or more and it's game over. Does that make sense?
So you want the balls to be destroyed and the variable totalballs to be set to 0 and keep the coroutine running normaly? If so just delete de StopCoroutine and StartCoroutine from the code i wrote and it should be fine. Just be sure to call it when you use the grenade.
Answer by melsy · Feb 02, 2018 at 09:33 PM
Are you using the coroutine just to use the timer function?
if so try this instead.
public class TEST : MonoBehaviour
{
public int totalballs = 0;
private GameObject bola;
private GameObject[] allballs;
private int i = 0;
float timer = 0;
float waitTime = 60;
public bool resetTimer = false;
private void Start()
{
timer = waitTime;
allballs = new GameObject[100];
//StartCoroutine(Destroy());
}
private void Update()
{
if(!resetTImer)
RunTimer();
else
timer = waitTime;
}
void RunTimer()
{
if (timer > 0)
{
timer -= Time.deltaTime;
}
else
{
if (totalballs > 6)
{
Time.timeScale = 0;
}
else if (totalballs <= 6 && totalballs > 0)
{
for (int j = 0; j < allballs.Length; j++)
{
if (allballs[j] != null)
{
Destroy(allballs[j].gameObject);
}
}
}
i = 0;
totalballs = 0;
timer = waitTime;
}
}
public void CountBalls(GameObject bola)
{
totalballs++;
allballs[i] = bola;
i++;
}
}
What this will do is run a timer for waitTime and if the bool gets set to true , the timer resets back to 0 and the timer stops. Call the bool true when you want to reset the timer and set it false when you want the timer to start running.