Search Unity

Lapsed values

Discussion in 'Scripting' started by magarcan, Feb 20, 2013.

  1. magarcan

    magarcan

    Joined:
    Sep 25, 2012
    Posts:
    34
    I have a value that is being updated inside my Update() function.

    I need to pick a value and after some time, compare with the current one.

    Code (csharp):
    1.  
    2.     void Start()
    3.     {
    4.         prevValue = 0;
    5.         InvokeRepeating("checkValue", 0, 0.5f);
    6.     }
    Code (csharp):
    1.  
    2.     void checkValue()
    3.     {
    4.         if (Mathf.Abs(prevValue - currentValue) > 10)
    5.             Debug.Log("Ok");
    6.        
    7.         prevAttitude = attitude;
    8.     }
    I don't know why, but it's not working properly. Any other idea about how to make this?

    Cheers!
     
  2. mjgood

    mjgood

    Joined:
    Jul 19, 2012
    Posts:
    14
    Hm, you mean something like this? The following script works just fine for me, so you must have something going on somewhere else in your code...

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class testScript : MonoBehaviour {
    6.    
    7.     float timeMe;
    8.    
    9.     void Start ()
    10.     {
    11.         InvokeRepeating("checkRepeat", 0, 0.5f);
    12.     }
    13.    
    14.     void Update ()
    15.     {
    16.         timeMe = Time.time;
    17.     }
    18.    
    19.     void checkRepeat()
    20.     {
    21.         if (timeMe >= 5)
    22.             Debug.Log ("This has gone on more than 5 seconds");
    23.     }
    24. }
    25.  
     
  3. magarcan

    magarcan

    Joined:
    Sep 25, 2012
    Posts:
    34
    I've just discovered that the problem was not in my code, it is in the variable.