using System;
using UnityEngine;

public class MonoUpdater : MonoBehaviour
{
    private Action _updateAction;
    private float _interval;
    private float _currentValue;

    public void StartUpdater(Action updateAction, float interval = 1f, bool actInstantly = true)
    {
        _updateAction = updateAction;
        _interval = interval;
        _currentValue = interval;
        enabled = true;
        if (actInstantly)
            updateAction?.Invoke();
    }

    public void StopUpdater()
    {
        enabled = false;
        _updateAction = null;
    }

    void Update()
    {
        _currentValue -= Time.deltaTime;
        if (_currentValue > 0)
            return;
        _updateAction?.Invoke();
        _currentValue = _interval;
    }
}
