Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 RedSuinit · Aug 01, 2014 at 02:43 AM · buttonvariablepress

A code to reset a variable after x time if another button is not pressed. Help!

Basically what I am trying to do is create a system were a certain variable will reset if one of 4 different buttons is not pressed within a certain time frame.

I was trying to do this with co-routines, but I have been unsuccessful without using the stop all coroutines function, which was fine until my I needed other coroutines for my character. I am struggling to figure out a new way to do this, can anyone help??

Below is my current script using the stop all coroutines function.

Thanks,

 void Update()
 {
 if (Input.GetButtonDown ("Fire State") && !onWall) 
 {
     if (morphCount <= morphLimit && !attacking) 
     {
         morphCount += 1;
         FireState ();
         StopAllCoroutines ();
         StartCoroutine (MorphReset());
     }
 }
 
 if (Input.GetButtonDown ("Water State") && !onWall)
 {
     if (morphCount <= morphLimit && !attacking) 
     {
         morphCount += 1;
         WaterState ();
         StopAllCoroutines ();
         StartCoroutine (MorphReset());
     }
 }
 
 if (Input.GetButtonDown ("Earth State") && grounded) 
 {
     if (morphCount <= morphLimit) 
     {
         morphCount += 1;
         EarthState ();
         StopAllCoroutines ();
         StartCoroutine (MorphReset());
     }
 }
 
 if (Input.GetButtonDown ("Powerless")) 
 {
     Powerless ();
     StopAllCoroutines ();
     StartCoroutine (MorphReset());
 }
 }
 
 IEnumerator MorphReset ()
 {
     yield return new WaitForSeconds (4);
     morphCount = 0;
 }


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 Ochreous · Aug 01, 2014 at 03:11 AM 0
Share

you'll want to create a float variable and check if it's count is less than 0. Then create an else statement below it.

     public float frameTime = 1.0f;    
     
     void Update () 
     {
         if (frameTime > 0) {
             frameTime -= Time.deltaTime;
         }
         else{
             $$anonymous$$orphCount = 0;
         }
     

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Eric5h5 · Aug 01, 2014 at 04:03 AM

Use StopCoroutine, so you can stop a specific coroutine, rather than using StopAllCoroutines. Either that or use Invoke instead of coroutines.

 if (condition) {
     CancelInvoke ("SomeFunction");
     Invoke ("SomeFunction", invokeTime);
 }
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
avatar image
0

Answer by Kiwasi · Aug 01, 2014 at 07:46 AM

You can use stop coroutine to stop a specific coroutine. You can only use StopCoroutine on a function you start with the string version

 // This works
 StartCoroutine("MorphReset");
 StopCoroutine("MorophReset");
 
 //This will not work
 StartCoroutine(MorphReset());
 StopCoroutine(MorophReset());
Comment
Add comment · Show 4 · 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 Kiwasi · Aug 01, 2014 at 08:00 AM 0
Share

Another solution using coroutines is as follows. This is a set and forget method. Use this by setting timeRemaining to 4 (or whatever other number) in place of you StartCoroutine.

 private float _timeRemaining;
 private float timeRemaining {
     set {
         if (_timeRemaining <= 0){
             _timeRemaining = value;
             StartCoroutine($$anonymous$$orphReset());
         } else {
             _timeRemaining = value;
         }
     }
     get {
         return _timeRemaining;
     }
 }
 
 IEnumerator $$anonymous$$orphReset ()
 {
     while (timeRemaining>0){
         timeRemaining -= Time.DeltaTime;
         yield return null;
     }
     timeRemaining = 0;
     morphCount = 0;
 }

You could go a step further and have this coroutine start in the set method for morphCount. That way every time you change morphCount it will automatically reset itself.

avatar image RedSuinit · Aug 01, 2014 at 01:58 PM 0
Share

Thanks Bored$$anonymous$$ormon, that looks like it will do what I want. The main issue that I have with just using StopCoroutine is that it seems as if it will only stop a coroutine started in the same statement. So for example:

 if(Input.GetButtonDown("Fire State"))
 {
     if(morphCount <= morphLimit)
     {
         morphCount += 1;
         FireState();
         StopCoroutine ($$anonymous$$orphReset());
         StartCoroutine ($$anonymous$$orphReset());
     }
 }

Lets say I have this code under all 4 states, this will not actually stop the coroutine currently running. The coroutines will just stack on top of each other and morphCount is continuously being reset to 0. Am I just missing something? Because I originally thought that this should have worked, with one stopcoroutine being called before each startcoroutine. It should have had only one morphreset running at any given time. However, this was not the case.

avatar image Kiwasi · Aug 01, 2014 at 08:07 PM 0
Share

To use stop you must call the coroutine via the string.

Edit: Whoops, just realised I didn't make this clear in my answer, will adjust.

avatar image RedSuinit · Aug 01, 2014 at 11:36 PM 0
Share

Ah, I see now.

So by calling it through the string it can be cancelled and will not stack. I see, thanks.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

gui.button down 2 Answers

Is there any way to move a GUI.Button across the screen (when clicked) with a script? 1 Answer

How to configure Gui Buttons and trigger2D correctly? 1 Answer

Action executed with more than one key? 1 Answer

Switch Camera On Button Press? 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