C# Event not firing the full function

I have a C# event that fires when a timer elapses. When that event fires I set GUI sliders to match the value. But the function that sets the sliders end abruptly on the first line and doesn’t finish the function. Does anyone have had this problem before?

EDIT: I have checked with the VSCode debugger and put two breakpoints at the first and second line of the function. The second breakpoint never triggers and no exception is being thrown in the console.

The function also works as intended when I use it in the Start function.

The sample code that I’m trying to use:

    private void SetNeedSliders()
    {
        _hungerSlider.value = _player.Needs.Hunger;
        // Doesn't fire after this line
        _thirstSlider.value = _player.Needs.Thirst;
        _sleepSlider.value = _player.Needs.Sleep;

        _hungerText.text = _player.Needs.Hunger.ToString();
        _thirstText.text = _player.Needs.Thirst.ToString();
        _sleepText.text = _player.Needs.Sleep.ToString();
    }

I fixed the problem. I was using native C# timers and the native C# event system. C# events inherently uses threads and you cannot manipulate Unity variables in threads. So when the event triggers it sets a boolean true which then the GUI gets updated in the Unity Update function.

Here’s a sample code:

    private void Start()
    {
            _player.Needs.needsChanged += ShouldSetNeedSliders;
    }

    private void Update()
    {
        if (_shouldSetNeedSliders)
        {
            SetNeedSliders();
            _shouldSetNeedSliders = false;
        }
    }

    private void ShouldSetNeedSliders()
    {
        _shouldSetNeedSliders = true;
    }

    private void SetNeedSliders()
    {
        _hungerSlider.value = _player.Needs.Hunger;
        _nutrientSlider.value = _player.Needs.Nutrient;
        _thirstSlider.value = _player.Needs.Thirst;
        _sleepSlider.value = _player.Needs.Sleep;

        _hungerText.text = _player.Needs.Hunger.ToString();
        _nutrientText.text = _player.Needs.Nutrient.ToString();
        _thirstText.text = _player.Needs.Thirst.ToString();
        _sleepText.text = _player.Needs.Sleep.ToString();
    }