Really strange behaviour on really simple script (Creating Ticking System)

I’m creating a ticks system for unity, and I’m breaking my head with something super weird in a super simple code.

1 - What do I want? Create a system that calls updates on other systems defined by a number of updates per second, even if it can cause lag in case the number of updates per second is very large.

2 - Based on this I created 2 scripts (Tickator.cs and Tickated.cs), in which the Tickator will be responsible for calculating how many ticks have passed to update the systems that are tickable.

//Tickator.cs
using UnityEngine;
[DefaultExecutionOrder(0)]  
public class Tickator : MonoBehaviour  
{  
    [Range(0, 240)] public uint TicksPerSecond;  
      
    public int TotalTicks;  
    public Tickated TickatedClass;  
      
    private float _t;  
  
    private void Update()  
    {  
        _t += Time.deltaTime * TicksPerSecond;  
        int elapsedTicks = (int) _t;  
  
        if (elapsedTicks > 0)  
        {  
            for (int i = 0; i < elapsedTicks; i++)  
            {  
                DoTick();  
            }  
              
            _t %= 1;  
        }  
    }  
  
    private void LateUpdate()  
    {  
        if (TotalTicks != TickatedClass.Ticks)  
        {  
            Debug.LogError("count of ticks on Tickator is different from count of ticks in Tickated!");  
            Debug.Break();  
        }  
    }  
  
    private void DoTick()  
    {  
        TotalTicks++;  
        TickatedClass.Tick();  
    }  
}
//Tickated.cs
using UnityEngine;  
public class Tickated : MonoBehaviour  
{  
    public int Ticks;  
  
    public void Tick()  
    {  
        Ticks++;  
    }  
}

3 - I added 2 debug variables one in each script to have control of how many ticks were ticked. And a Debug.Break () that will pause the editor and throw an error when the ticks between Tickator and Tickated are different (they should never be).

4 - I put both in a gameobject and assign TIckated to the field TickatedClass

5 - If I hit play everything works perfectly, the amount of ticks between ticker and ticked never changes (perfect).

6 - While in play mode, if I hit pause, then change the value of TicksPerSecond and return to play, the amount of ticks between tickator and tickated never changes (also perfect).

7 - But if I change this value while in playmode soon the amount of ticks between tickator and tickated changes (Whyyy)!

8 - What is more strange is, I created a class that changed the TicksPerSecond value of the Tickator that I called Setter.cs, when I change the value through it, updating the TicksPerSecond value of the Tickator through the MonoBehaviour Update event, the error does not occur. The same happens if I change the value through the game by Sliders or InputFields.

//Setter.cs
using UnityEngine;  
[DefaultExecutionOrder(-1000)]  
public class Setter : MonoBehaviour  
{  
    [Range(0, 240)] public uint Value;  
    public Tickator m_Tickator;  
      
    private void Update()  
    {  
        m_Tickator.TicksPerSecond = Value;  
    }  
}

9 - I also tried changing the order of execution between Tickator.cs and Setter.cs using DefaultExecutionOrder, to see if the same error happened, but it did not happen …

[135727-tickingsystem.zip|135727]

Can someone help me understand what’s going on? Thanks in advance,

Update function works with Frames/Seconds, if you want a time controlled kind of update function, you can simply use fixed update and you can control that time from the Fixed timesteps (Edit->Project settings->Time), by default fixed update is called every 0.02 seconds, you can change this value.

public int frameCounters;
public int counts;
void Start () {
frameCounters = new int {204,824,2456,10000,10202};
counts = new int[frameCounters.Length];

	}
	
	// Update is called once per frame
// Change to FixedUpdate for time counting
	void Update () {
		int i = counts.Length;
		while(i>0){i--;
			counts*++;*

if(counts_>=frameCounters*){
print(“counter number:”+ i+ " has reached:"+counts+" frames");
counts=0;
} }}*_

The thing is, how is it possible that the count of ticks in the Tickator could be different from the count of ticks in Tickated? Because both are iterated in the same method…