How to only update once in Update function?

I am trying to increase count value when color of button is blue and decrease it when when color is green.
The problem is that count value is increasing non stop when color is blue and decreasing non stop when its green. I am trying that count value should only increase or decrease once when color changes and wait for next color change to change its value.

Here is my code:

void Update()
{
	itemInfo.text = itemName + " (" + count + ")" + "

Cost: " + cost + "
Power: +" + clickPower;
slider.value = click.gold / cost * 100;

	if (slider.value >= 100) 
	{
		GetComponent<Image> ().color = affordable; //blue color
		check = true;

	}  
	else 
	{
		GetComponent<Image> ().color = standard; // green color
		check = false;
	} 

	if (check == true) 
	{
		count++;
	} 
	else if (check == false) 
	{
		count--;

		if (count< 0) 
		{
			count= 0;
		}
	}
}

Thank You.

The problem here is you’re using the Update function for something that shouldn’t be done in an update function. It’s like trying to do something in the Start function that should be done only when an object is destroyed. Update function is for something that should be done on every frame or during most of the frames. When you try to do something only once in a while you end up doing complicated things with bools.

The cleanest solution is: don’t do it in the Update function. I’m not sure how your game works so it’s hard to show you a working solution, but I suggest you try to refactor your script with this idea. Maybe click.gold is the thing that changes, so instead of checking it’s value in the update function you can call a function after click.gold changes, and in that function you check if the count should change.

Anyway, in your code you can fix the problem checking the previous value of the slider against the new one, If both are greater than 100 or both are less than 100, then you shouldn’t count anything:

     void Update()
     {
         itemInfo.text = itemName + " (" + count + ")" + "

Cost: " + cost + "
Power: +" + clickPower;

         float oldValue = slider.value;
         slider.value = click.gold / cost * 100;

         if (slider.value >= 100 && oldValue < 100) 
         {
             GetComponent<Image> ().color = affordable; //blue color
             count++;
         }  
         else if (slider.value < 100 && oldValue >= 100)
         {
             GetComponent<Image> ().color = standard; // green color
             if (count > 0) 
             {
                 count--;
             }
         }
     }

private bool lastWasIncrease;

void Update()
 {
     itemInfo.text = itemName + " (" + count + ")" + "

Cost: " + cost + "
Power: +" + clickPower;
slider.value = click.gold / cost * 100;
if (slider.value >= 100)
UpdateCount(true);
else
UpdateCount(false);
}
private void UpdateCount(bool increase)
{
if(increase && !lastWasIncrease )
{
count++;
lastWasIncrease = true;
GetComponent ().color = affordable; //blue color
}
else if(!increase && lastWasIncrease )
{
count–;
lastWasIncrease = false;
GetComponent ().color = standard; // green color

        if (count < 0) 
            count = 0;
    }
}