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 /
avatar image
0
Question by Derpybunneh · Sep 17, 2016 at 06:21 AM · c#coroutinetimer

If condition is not true, stop timer and reset.

Hello! What I'm trying to do is create a block hardness script using a timer. Originally I was using a float variable that would be decreased when mined, but if a computer has a higher FPS it ends up mining really fast. I thought I could try using a timer. This is where I'm getting stuck. I can't get the timer to stop when I'm not mining the block. It just keeps going regardless of whether I'm still mining. I want it so that the timer only runs when I'm mining the block. If I'm mining and then suddenly stop before the timer is up, I want it to reset, and not run again until I mine again.

I've tried coroutine, I've tried timers, but it just doesn't seem to work. I don't get why it is so hard to simply stop a timer if a certain condition is not true.

Here's some of my block destruction code, the code with a variable being subtracted:

 hit.collider.gameObject.GetComponent<BlockProp>().strength = hit.collider.gameObject.GetComponent<BlockProp>().strength - drillStrength;
 
 if (hit.collider.gameObject.GetComponent<BlockProp>().strength <= 0) {
 (destroys block in here, code here not neccesary)
 }


Comment
Add comment · Show 1
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 Sergio7888 · Sep 17, 2016 at 10:45 AM 0
Share

to stop a function use return; or return null; or return default(TypeName);, to stop a for or while use break;, to stop a coroutine use yield break;.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by beefyt123 · Sep 17, 2016 at 07:42 AM

Hey I just tried this script out for you. From what I understand of your original question it should do what you want. You will need to change it to match what your needs.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class TimerResetScript : MonoBehaviour {
 
     float strength = 10;
 
     public bool isBeingMined;
 
     void OnMouseDown()
     {
         //When mouse clicks on block sets bool to true and starts the Coroutine
         isBeingMined = true;
         StartCoroutine(mineBlock());
     }
 
     void OnMouseUp()
     {
         //When mouse is released sets bool to false and stops the Coroutine
         isBeingMined = false;
         StopCoroutine(mineBlock());
     }
 
     IEnumerator mineBlock()
     {
         //Checks to see is the bool is true and if so increment its strength down.
         while (isBeingMined)
         {
             if (strength > 0)
             {
                 strength = strength - Time.deltaTime;
                 Debug.Log("strength: " + strength);
 
             }
             else if (strength <= 0)
             {
                 Destroy(this.gameObject);
             }
             yield return null;
         }
         yield return null;
     }
 }

It doesn't reset the timer but it will just stop reducing the strength of the block while the player is holding the mouse button down.

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

222 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 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 avatar image avatar image avatar image avatar image

Related Questions

C# simple delay execution without coroutine? 2 Answers

Distribute terrain in zones 3 Answers

Making a fill take exactly n seconds to complete 2 Answers

Unexpected coroutine behavior for simple timer 2 Answers

Create a countdown in C# (inside Coroutine) 2 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