Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by EvanCheddar · Feb 02, 2018 at 11:17 PM · gameobjectresetcounterstartcoroutinedestory

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;
             }
         }    
 
     }
 }
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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.

Comment
Add comment · Show 12 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Obsessi0n · Feb 02, 2018 at 08:14 PM 0
Share

Fixed the code had StopCoroutine ins$$anonymous$$d of StartCoroutine.

avatar image EvanCheddar Obsessi0n · Feb 02, 2018 at 09:15 PM 0
Share

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?

avatar image Obsessi0n EvanCheddar · Feb 02, 2018 at 09:42 PM 0
Share

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.

Show more comments
avatar image
1

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

109 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Reset GameObject with trigger on respawn 1 Answer

destroy all game objects that are touching the same collider? 1 Answer

Reset rotation of gameobject 1 Answer

Spawning a number of enemies while condition is true 0 Answers

SetActive(false), and then back to true to call OnEnable 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges