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 Elessar96 · Feb 19, 2021 at 11:57 AM · lightingtimerinvokerepeating

InvokeRepeating work only once

Hi everyone, i read some posts here and in other forums but i don't understand how to use InvokeRepeating.

public class LightMovements : MonoBehaviour {

 public Light myLight;
 private float maxRange = 25;
 private float minRange = 13;
 private float maxIntensity = 5;
 private float minIntensity = 4;
 private float midRange = 19;
 private float midIntensity = 4;
 private bool timerDecrease = false;

 [SerializeField] float timer;

 void Start()
 {
     myLight = GetComponent<Light>();   //Gli passiamo il componente luce        
     timer = 0f;
 }

 void Update()
 {
     if (Input.GetKey(KeyCode.E))    //Utilizziamo la E per incrementare i parametri della luce 
     {
         timer += Time.deltaTime;
         if (timer >= 0.5f)
         {
             myLight.intensity = maxIntensity;
             myLight.range = maxRange;
         }
     }

     if (Input.GetKeyUp(KeyCode.E))   //A regola nel momento in cui alzi il dito dalle E abbassa tutto 
                                         // nel giro di qualche secondo, peccato lo faccia solo la prima volta circa
     {
         if (!IsInvoking("RangeDecreaser"))
         {
             InvokeRepeating("RangeDecreaser", 0.6f, 0.1f);
         }
         if (!IsInvoking("IntensityDecreaser"))
         {
             InvokeRepeating("IntensityDecreaser", 0.6f, 0.6f);
         }
         timer = 0;

     }
     
     if (Input.GetKey(KeyCode.Q))      //Utilizziamo la Q per diminuire i parametri della luce 
     {
         timer += Time.deltaTime;
         if (timer >= 0.5f)
         {
             myLight.intensity = minIntensity;
             myLight.range = minRange;
         }

     }

     if (Input.GetKeyUp(KeyCode.Q))   //Stessa cosa delle E ma aumentiamo a regola
     {
         if (!IsInvoking("RangeIncreaser"))
         {
             InvokeRepeating("RangeIncreaser", 0.6f, 0.1f);
         }
         if (!IsInvoking("IntensityIncreaser"))
         {
             InvokeRepeating("IntensityIncreaser", 0.6f, 0.6f);
         }
         timer = 0;
     }

 }
 void RangeDecreaser()
 {
     while (myLight.range > midRange)
     {
         myLight.range -= 1;
     }
   
 }

 void IntensityDecreaser()
 {
     while (myLight.intensity > midIntensity)
     {
         myLight.intensity -= 1;
     }
 }

 void RangeIncreaser()
 {
     while (myLight.range < midRange)
     {
         myLight.range += 1;
     }
 }
 
 void IntensityIncreaser()
 {
     while (myLight.intensity < midIntensity)
     {
         myLight.intensity += 1;
     }
 }

}

This is my script. What I would like is that after the light has increased its parameters, after a few tenths of a second it returns to mid values. I tried it with Invoke but it works the first time then the others repeats at every frame. Can you advise me how to correct this or maybe you have to advise me in some smarter way? thank you so much,

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 Elessar96 · Feb 19, 2021 at 11:58 AM 0
Share

I apologize for the comments in Italian but they are not so important

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by unity_ek98vnTRplGj8Q · Feb 19, 2021 at 07:24 PM

If you have while loops in your coroutine make sure that you also use yield return if you want the while loop to run over time. Currently,

void RangeDecreaser() { while (myLight.range > midRange) { myLight.range -= 1; }

}

Just executes the whole while loop all at once. If you want your while loop to run over time then use a yield return null, which basically just means "ok I'm going to stop executing for now and I'll pick back up where I left off on the next update frame"

  while (myLight.range > midRange)
  {
      myLight.range -= 1;
      yield return null;
  }

However, you are using InvokeRepeating, which means that Unity is going to repeatedly call your function. If this is what you want to do, you shouldn't have a while loop at all because InvokeRepeating will call the function again for you. Just change it to an if condition

  if (myLight.range > midRange)
  {
      myLight.range -= 1;
  }

So either use just Invoke with a while loop and a yield return, or use InvokeRepeating with an if statement. If you want to use the first method with yield return, you can control the timing between calls by returning WaitForSeconds instead, so to match the frequency of your InvokeRepeating call you would say

  while (myLight.range > midRange)
  {
      myLight.range -= 1;
      yield return new WaitForSeconds(0.1f);
  }
Comment
Add comment · Show 1 · 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 Elessar96 · Feb 21, 2021 at 02:13 PM 0
Share

Thank you so much, i'll try asap. For now i found myself another solution using isInvoking and CancelInvoke

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

154 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

Related Questions

Perfect sinus 0 Answers

Flashlight Timer Help 1 Answer

Cannot Get Light To Flash - Help 1 Answer

Subtract one from health every five seconds. 1 Answer

Loop a function, once per second, X number of times 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