i am making an increment of one when user clicks a game object, trying to decrement the counter if the same game object is clicked again

i am making an increment of one when user clicks a game object, trying to decrement the counter if the same game object is clicked again. here is my code

void Update() 
{
 if( Input.GetMouseButtonDown(0) ) { Ray ray = Camera.main.ScreenPointToRay( Input.mousePosition );
 RaycastHit hit;
     if( Physics.Raycast( ray, out hit, 100 ) )
     {
         if( hit.transform.gameObject.name == "answer" )
             count++;
         print(count);                
     }
     }
 
 }

Yo,

Maybe you can use a Dictionnary (System.Collection.Generic)

Dictionnary<string, int>

also can store GameObject as id

Dictionnary<GameObject, int>

But if you can add a component on all wanted gameObjects, you might use the method “OnMouseOver” from MonoBehaviours

public class Foo : MonoBehaviour
{
	[SerializeField]
	private int foo = 0;

	private void OnMouseOver()
	{
		if(Input.GetMouseDown(0))
		{
			if(foo == 0)
			{
				foo++;
			}
			else
			{
				foo--;
			}
		}
	}
}