Coroutine messes with SyncVar?

Hi,

The following code should update a value on the clients with a slight delay. Problem is that on my host client it triggers an infinity loop. Because the setting of the new value for testVariable in the Coroutine triggers another update of testVariable and a call of the OnChange method, which then starts the whole new cycle.

When i set the new value for testVariable directly in the hook method, no update will be triggered, of course.

If I “outsource” the setting of a new value for testVariable to a Coroutine it will trigger an update.

Is this a bug? Or am I doing something wrong here?

[Syncvar (hook="OnChange")]
int testVariable;

OnChange(int value)
{
    StartCoroutine(DelayedValueChange());
}


IEnumerator DelayedValueChange()
{
     float duration = 0f;
     while(duration < 3f)
    {
        duration += Time.Delta;
        yield return null; 
    }
    testVariable = int;
}

Yes, you are causing an infinite loop when you set testVariable inside the DelayedValueChange() function, which is triggering OnChange again.

You want to have two values. One that you are displaying and the real current value.

int displayValue;     // displayed to the user with delay
[Syncvar (hook="OnChange")] int realValue;    // real value

OnChange(int value)
{
     realValue = value;
     StartCoroutine(DelayedValueChange());
}
 
IEnumerator DelayedValueChange()
{
      float duration = 0f;
      while(duration < 3f)
      {
         duration += Time.Delta;
         yield return null; 
      }
     displayValue= realValue;
}