Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Invoke and Time.timeScale

Discussion in 'Scripting' started by bdev, Mar 18, 2011.

  1. bdev

    bdev

    Joined:
    Jan 4, 2011
    Posts:
    656
    Just looking for a quick answer, Does timeScale affect Invoke/InvokeRepeating rate?
     
  2. bigmisterb

    bigmisterb

    Joined:
    Nov 6, 2010
    Posts:
    4,221
    The easiest way to answer that.... make a test...

    Code (csharp):
    1.  
    2. Function Start(){
    3. Invoke("MyAnimation", 2.0);
    4. Time.timeScale=0.0;
    5. }
    6.  
    If the Invoke happens, no, if not, yes.
     
  3. Deeweext

    Deeweext

    Joined:
    Jan 25, 2013
    Posts:
    10

    Now, without spoiling the result, it might help if you also notice the syntax-typo in function.
     
  4. gary_bbgames

    gary_bbgames

    Joined:
    Jan 20, 2012
    Posts:
    22
    To save anyone else just looking for info and without access to Unity to check the result, Time.timeScale = 0.0f will stop Invokes from being called until it is set to a value other than 0.0f.
     
  5. Nition

    Nition

    Joined:
    Jul 4, 2012
    Posts:
    781
    To give a more specific answer, Invoke does take time.timeScale into account (it operates on game time, not real time).

    Test code:
    Code (csharp):
    1.  
    2. float startTime;
    3.  
    4. void Start() {
    5.     startTime = Time.realtimeSinceStartup;
    6.     Invoke("TestMethod", 5.0f);
    7. }
    8.  
    9. void TestMethod() {
    10.     print("Real time taken 1: " + (Time.realtimeSinceStartup - startTime));
    11.     startTime = Time.realtimeSinceStartup;
    12.     Time.timeScale = 0.5f;
    13.     Invoke("TestMethod2", 5.0f);
    14. }
    15.  
    16. void TestMethod2() {
    17.     print("Real time taken 2: " + (Time.realtimeSinceStartup - startTime));
    18. }
    19.  
    takes 5 seconds for the first Invoke, and 10 for the second.
     
    vonSchlank likes this.
  6. Sonoshee

    Sonoshee

    Joined:
    Jul 8, 2014
    Posts:
    77
    Hey guys, I need Invoke() to be independent from the timeScale, but rather on the real time since startup. Is there any way to make it possible ?
     
    emredesu, awsapps and klesun like this.
  7. Sbizz

    Sbizz

    Joined:
    Oct 2, 2014
    Posts:
    250
    Code (CSharp):
    1. public void InvokeRealTime(string name, float duration) {
    2.     Invoke(name, duration / Time.timeScale);
    3. }
     
    DragonTail123b likes this.
  8. novashot

    novashot

    Joined:
    Dec 12, 2009
    Posts:
    373

    What happens when time scale is set to 0? Last I checked most math goes crazy when dividing by 0...
     
    emredesu and klesun like this.
  9. Sbizz

    Sbizz

    Joined:
    Oct 2, 2014
    Posts:
    250
    Code (CSharp):
    1. public void InvokeRealTime(string name, float duration) {
    2.     Invoke(name, Time.timeScale == 0 ? 0 : duration / Time.timeScale);
    3. }
     
    novashot likes this.
  10. or1on

    or1on

    Joined:
    Nov 22, 2013
    Posts:
    4
    ..still, if the timescale changes in the meantime (between the instant when you call Invoke and <duration> seconds later), the waiting time gets screwed up.
    The only workaround I found is starting a coroutine and performing a realtime wait (checking against Time.realtimeSinceStartup every frame).
     
  11. novashot

    novashot

    Joined:
    Dec 12, 2009
    Posts:
    373
    Can always do a CancelInvoke and Invoke again to avoid real-time monitoring.
     
  12. Yuanfeng

    Yuanfeng

    Joined:
    Feb 14, 2014
    Posts:
    2
    it will can a error when Time.timescale is 0, duration / Time.timescale get a infinite value...

    There has a tween plugin call DoTween ,and it provide a function call DOVirtual.DelayedCall. and you can decide wether ignore timescale or not..
    follow is the assetstore link with the document links in the webpage.
    https://www.assetstore.unity3d.com/en/#!/content/27676
     
    p_lewis, Draag and sohailciit38 like this.
  13. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,748
    Code (csharp):
    1. public void InvokeRealTime(string functionName, float delay) {
    2. StartCoroutine(InvokeRealTimeHelper(functionName, delay) );
    3. }
    4. private IEnumerator InvokeRealTimeHelper(string functionName, float delay) {
    5. float timeElapsed = 0f;
    6. while (timeElapsed < delay) {
    7. timeElapsed += Time.unscaledDeltaTime;
    8. yield return null;
    9. }
    10. SendMessage(functionName);
    11. }
    12.  
     
    joaodeconto likes this.
  14. samson212

    samson212

    Joined:
    Feb 23, 2013
    Posts:
    1
    Couldn't you just use System.Timers.Timer for that?
     
  15. Sonoshee

    Sonoshee

    Joined:
    Jul 8, 2014
    Posts:
    77
    I ended up doing something similar to that with timers. Thanks anyways!
     
  16. SweatyChair

    SweatyChair

    Joined:
    Feb 15, 2016
    Posts:
    140
    Use coroutine with UnityAction and WaitForSecondsRealtime:

    Code (csharp):
    1.  
    2. StartCoroutine(InvokeRealtimeCoroutine(DoSomething, seconds));
    3.  
    4. private IEnumerator InvokeRealtimeCoroutine(UnityAction action, float seconds)
    5. {
    6.     yield return new WaitForSecondsRealtime(_seconds);
    7.     if (action != null)
    8.         action();
    9. }
    10.  
     
  17. trava

    trava

    Joined:
    Feb 25, 2013
    Posts:
    4

    Are you sure, here need divide? Maybe multiply?
     
    hufi9000 likes this.
  18. Catzeromeio

    Catzeromeio

    Joined:
    Nov 18, 2016
    Posts:
    7
    try this:
    Code (CSharp):
    1.  public IEnumerator Invoke( System.Action action, float Delay)
    2.     {
    3.         yield return new WaitForSecondsRealtime(Delay);
    4.         if (action != null)
    5.             action();
    6.     }
     
  19. hk1ll3r

    hk1ll3r

    Joined:
    Sep 13, 2018
    Posts:
    88
    Ziplock9000 and sp-LeventeLajtai like this.
  20. Lurking-Ninja

    Lurking-Ninja

    Joined:
    Jan 20, 2015
    Posts:
    9,991
  21. Redrag

    Redrag

    Joined:
    Apr 27, 2014
    Posts:
    175
    I agree this should multiply not divide. I do this:
    InvokeRepeating ("GetServerDateTime", 15 * Time.timeScale, 15 * Time.timeScale);

    Thanks for the tip on WaitForSecondsRealtime - I have never noticed that!