Hello,
I have asked a question similar to this one before (HERE) and as I said, I managed to create a ticking function that ticks @1 milisecond with System.Timers.
I have a script named GameLogic tied to an empty game object (of course inheriting from MonoBehaviour) and in that script in the function Start(), I made a new timer like this:
Timer timer;
void Start()
{
timer = new Timer();
timer.Interval = 1; // 1 milisecond
timer.Elapsed += timer_Elapsed;
timer.Start();
}
void timer_Elapsed(object sender, ElapsedEventArgs e)
{
// Update all connections between chips
updateWireConnections();
}
I also have a Chip script tied to every single chip I have instantiated in the game, and I bind another timer event in that Chip script from GameLogic script like this:
GameLogic gameLogic;
bool gameLogicLoaded = false;
void Update()
{
if (!gameLogicLoaded)
{
gameLogic = GameObject.FindGameObjectWithTag("Game").GetComponent<GameLogic>();
if (gameLogic != null)
{
gameLogic.timer.Elapsed += timer_Elapsed; // Here I bind another timing event from the timer object in GameLogic script
gameLogicLoaded = true;
}
}
}
void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
ChipTick(); // this is a virtual function. I can then make scripts that inherrit from this class and override the ChipTick()
}
public virtual void ChipTick() { }
The main problem here is that timer object from GameLogic script stops responding at random times and the chips stop updating. I get no errors in the console though.