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 chainylol · Dec 16, 2017 at 07:41 PM · programmingvariablesfloatinvokerepeating

InvokeRepeating help

Hey everyone, i have a problem with InvokeRepeating and variables. I have a float called hunger, and im using an InvokeRepeating to make sure the hunger float decreases every x seconds. This works fine, but what i want is for it to decrease even faster if i hold down a button. Therefor, i obviously use variables, which you can see in my code below. When i press said button, i can see the variables change in the inspector, but the hunger float is actually not decreasing any faster. Is this some sort of limitation w/ InvokeRepeating? Or is something wrong with my code?

C#:

     public TextMeshProUGUI hungerText;
     public float hunger;
     public float hungerDelay;
     public float hungerRate;
 
     // Use this for initialization
     void Start () {
         //hungerDelay = 0.5f;
         hunger = 100;
         InvokeRepeating ("HungerDecay", hungerDelay, hungerRate);
 
     }
     void Update () {
     hungerText.text = "" + hunger;

     //hungerSpeed -= 0.1f;

     if (Input.GetKey (KeyCode.LeftShift)) {
         hungerRate = 0.0001f;
     }

     void HungerDecay()
     {
         hunger -= 1;
     }
 }

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 Nikaas · Dec 16, 2017 at 08:27 PM 0
Share

Once the value is passed to invoke, it's done. You can try coroutine loop and change its wait time - BUT the new time will be applied on the next "cycle" (i.e once current wait starts it is unchangeable similar to invoke, the smaller wait value will be applied for the next wait period). If you want it completely responsive then maybe do your own ti$$anonymous$$g in Update(). Probably something like hungerTimer += Time.deltaTime * speedCoefficient , with the coefficient being 1 on released button and some other value on pressed button. Then when timer reaches X change hunger by Y and reset hungerTimer to 0.

2 Replies

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

Answer by Prastiwar · Dec 16, 2017 at 08:00 PM

You need to CancelInvoke and set it again after changing variable.

 if (Input.GetKey(KeyCode.LeftShift))
         {
             if (hungerRate == yourBaseRate) // to make sure instruction will happen only once
             {
                 CancelInvoke("HungerDecay");
                 hungerRate = 0.0001f;
                 InvokeRepeating("HungerDecay", hungerDelay, hungerRate);
             }
         }
         if (Input.GetKeyUp(KeyCode.LeftShift))
         {
             CancelInvoke("HungerDecay");
             hungerRate = yourBaseRate;  // so you can set to "not holding" variable.
             InvokeRepeating("HungerDecay", hungerDelay, hungerRate);
         }
Comment
Add comment · Show 2 · 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 chainylol · Dec 16, 2017 at 08:40 PM 0
Share

Took me a while to understand it lol but your answer absolutely works! Thanks a lot :)

avatar image Nikaas · Dec 16, 2017 at 08:59 PM 0
Share

Wouldn't this generate garbage and reset the repeat timer on each shift press/release?

avatar image
0

Answer by vardevelopment · Dec 16, 2017 at 08:00 PM

I would do something like this:

  public TextMeshProUGUI hungerText;
      public float hunger;
      public float hungerDelay;
      public float hungerRate;
      public float originalHungerRate;
  
      // Use this for initialization
      void Start () {
          //hungerDelay = 0.5f;
          originalHungerRate = hungerRate;
          hunger = 100;
          InvokeRepeating ("HungerDecay", hungerDelay, hungerRate);
  
      }
      void Update () {
      hungerText.text = "" + hunger;
      //hungerSpeed -= 0.1f;
      if (Input.GetKey (KeyCode.LeftShift)) {
          CancelInvoke();
          hungerRate = 0.0001f;
          InvokeRepeating ("HungerDecay", hungerDelay, hungerRate);  
      } else if (Input.GetKeyUp (KeyCode.LeftShift)) {
          CancelInvoke();
          hungerRate = originalHungerRate;
          InvokeRepeating ("HungerDecay", hungerDelay, hungerRate);
      } 
      void HungerDecay()
      {
          hunger -= 1;
      }
  }


I don't know entirely if this works or not but maybe it will.

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

79 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

Related Questions

Multiple Cars not working 1 Answer

random respawn and respawn delay 1 Answer

Problem with swithing weapons in a list of scriptable objects 1 Answer

Accessing variables from other scripts 0 Answers

Initilization of variables in shader 1 Answer


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