How to execute different actions on the same button (code included)

Hi all, I have one button (or tap on screen) that executes a different action (e.g. shoot, pull lever, eat) depending on a necessary condition (has gun, has lever, has cake) otherwise executes a default action (jump).
Two actions shouldn’t happen at the same time and I want a delay between the actions

This is the file that translates the button/tap into either an action or a jump, and adds a delay. And isn’t working

public class PlayerActionManager : MonoBehaviour
{
    private bool _isJumping;
    public bool IsJumpig { get => _isJumping;  }
    private PlayerInput _playerInput;
    private List<IActionable> _actionables = new List<IActionable>();
    private float _timer;

    private void Start()
    {
        var actionable = FindObjectsOfType<MonoBehaviour>().OfType<IActionable>();
        foreach (IActionable a in actionable)
        {
            _actionables.Add(a);
        }
        _playerInput = GetComponent<PlayerInput>();
    }


    private void Update()
    {
        foreach (IActionable actionable in _actionables)
        {
            if (actionable.CanExecuteAction)
            {
                if (_playerInput.ActionInput)
                {
                    actionable.ExecuteAction();
                    _timer = 1f;
                }
            }  
            else
            {
                if (_timer < 1f)
                {
                    _timer += Time.deltaTime;
                }
                else
                {
                    _isJumping = _playerInput.ActionInput;
                    _timer = 0;
                }
            }
        }
     }
}

Here’s the rest of the setup (simplified code)

public class PlayerInput : MonoBehaviour
{
public bool ActionInput
    {
        get
        {
            if (_touchInput != null)
            {
                if (_touchInput.screenTouched)
                {
                    return true;
                }
            }
            return Input.GetKey(KeyCode.UpArrow);
        }
    }
}
public class CharachterMovement : MonoBehaviour
{
 void Update()
    {
        Move(_playerActionManager.IsJumping);
    }
}
  public void Move(bool canJump)
  {

   if (IsGrounded && canJump)
        {
              IsGrounded = false;
            _rigidbody2D.AddForce(new Vector2(0f, _jumpForce));
        }
      }
}
public interface IActionable
{
    bool CanExecuteAction { get; }
    void ExecuteAction();
}
public class PickUpManager : MonoBehaviour, IActionable
{
    private bool _isLoaded;
    public bool CanExecuteAction { get => _isLoaded; }

    private void OnTriggerEnter2D(Collider2D collision)
    {
        _isLoaded = true;
        }
    }

    private void Update()
    {
        if (_canThrow)
        {
            _throwable.Throw();
                _isLoaded = false;
                _canThrow = false;
            }
        }
    }

    public void ExecuteAction()
    {
        _canThrow = true;
    }
}

Break things apart a bit.

  • Capture the intent to tap a button into a variable.

  • Operate your cooldown timer(s)

  • If the timers are proper to allow input, then:

---- decide what to do (select only ONE action if that’s what you want)

---- do it

---- set any timer values you need

Thank you for your answer.

Yes, that I have in _playerInput.ActionInput

But If I operate the cooldown first, I’d have a delay executing in the first action, wouldn’t I? I want the first action to trigger right away, and then have a delay before the next action.

One issue I found is that all the actions (jump, shoot, pull lever, eat) happen in the update and that the player input is read in update too. So if the button is not being pressed anymore (false) this interrupts the action, but instead I want the action to continue.

My code above attempts to model this:

if a button is pressed
if the player can jump (e.g. none of the other action conditions are true)
then the player jumps
if the player can’t jump (e.g. any of the other action condition is true)
then the action starts playing until the end
the button can be pressed again only after x seconds