'how do i increase a value by 1 once by checking current condition

im making a game based on pico park, i've been trying to replicate the box mechanic(x players needed to move a box) but i have a problem, i have a float called "playersneeded" that decreases when a player touches a box and increases when it gets out of collision(using OnCollisionEnter/Exit), but i dont know how to update that value based of if im currently pushing or not the box, i've been trying for a week to find a way to do it but i cant find anything, i've tried using OnCollisionStay for it but the value keeps incrementing. currently this is the code i use for the box:

using UnityEngine;
public class Box : MonoBehaviour 
{
   private GameObject player;
     private GameObject number;
   public bool Pushed;
  
 void Start() 
 {
     player = GameObject.FindWithTag("Player");
     number = GameObject.Find("Box/number");
 }

 private void OnCollisionEnter2D(Collision2D other) 
 {
     if (other.gameObject.tag == "Box" && other.transform.position.y > transform.position.y)
     { 
                 other.transform.SetParent(transform);
     }
     if (other.gameObject.tag == "Player" && other.transform.position.y < transform.position.y)
     {
         Pushed = true;
         GetComponentInChildren<NumberChanger>().currentPlayers++;
         GetComponentInChildren<NumberChanger>().PlayersNeeded--;
         gameObject.transform.GetComponent<Rigidbody2D>().bodyType = RigidbodyType2D.Dynamic;

     }
 }
 
 private void OnCollisionStay2D(Collision2D other)
 {
 if (other.gameObject.tag == "Box" && other.transform.position.y > transform.position.y)
     { 
                 other.gameObject.transform.GetComponent<Rigidbody2D>().bodyType = RigidbodyType2D.Kinematic;
     }    
     if (other.gameObject.tag == "Player" && other.transform.position.y < transform.position.y)
     {
         Pushed = true;
     
         gameObject.transform.GetComponent<Rigidbody2D>().bodyType = RigidbodyType2D.Dynamic;

     }
      if (other.gameObject.tag == "Player" && player.GetComponent<playercontroller>().IsPushing == false && other.transform.position.y < transform.position.y)
     {
      Pushed = false;
     
     }
     
 }    
 
 private void OnCollisionExit2D(Collision2D other) 
 {
     if (other.gameObject.tag == "Box") 
     {
                 other.transform.SetParent(null);
     }
      if (other.gameObject.tag == "Player" && player.GetComponent<playercontroller>().IsPushing == false && other.transform.position.y < transform.position.y)
     {
      Pushed = false;
      GetComponentInChildren<NumberChanger>().currentPlayers--;
      GetComponentInChildren<NumberChanger>().PlayersNeeded++;
     }
    
 }
 
}

to be clear, i want it to increase by 1 if the player is currently touching the box but not moving and decrease by 1 if the player is touching and moving it. hope someone can help me. thanks



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source