Increment function skips number 2.

I have this script which is supposed to increment or decrement the number when I press the right buttons. When I increment it for the first time it says 2 and then 3 but after that, when I decrement it, it says 1 and when I increment it it says 3, completely skipping the number 2. Both UI buttons have this script attached and are using the right functions. Images are correctly assigned. Also, even though the number is 3, it doesn’t change the Image to img3. Here’s the code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class selector : MonoBehaviour
     int counter = 1;
    public Image img1;
    public Image img2;
    public Image img3;
    void Start()
        img1.enabled = true;
        img2.enabled = false;
        img3.enabled = false;

      public void Incrementer() 
        if (counter > 0 && counter < 3)
        { counter ++; }
    public void Decrementer()
        if (counter > 1 && counter < 4)
        { counter --; }
    void Update()
        if (counter == 1) { img1.enabled = true; img2.enabled = false; img3.enabled = false; }
        if (counter == 2) { img1.enabled = false; img2.enabled = true; img3.enabled = false; }
        if (counter == 3) { img1.enabled = false; img2.enabled = false; img3.enabled = true; }

Let me see if I understand your setup correctly:

You have in your UI 2 buttons, one is supposed to increment a counter, the other is supposed to decrement. You are displaying a counter which shows numbers 1-3 (img1, img2, img3). You have the above script attached to both the increment and decrement buttons, and on each one you’ve linked their OnClick to either Incrementer() or Decrementer(). On both instances of the script you have hooked up the references to img1-3.

If my understanding is accurate, here is what your problem likely is:

By attaching the script to both buttons, you are running 2 instances of this script simultaneously. That means both buttons have their own ‘counter’ variable and are both running their Update() method independently of each-other. So when you click the Increment button, its counter goes up to 2, but the Decrement button’s counter stays at 1. Since they are both referencing the same img1-3, they will ‘fight’ over which image is enabled. Depending on which one’s Update() runs last, you’ll see varying results on what number your counter image displays, hence the weird behavior you are describing.

To fix:

You should structure your game object hierarchy such that you have a parent object named ‘Counter’ or something, and attach your script to that. The Increment and Decrement buttons, and img1, img2, img3 will go under it. Then the OnClick events for each button should reference the same script on the ‘Counter’ object. In other words, there should be only 1 game object with your script attached to it, per counter that you want to display.

Hope this helps! If I’m assuming incorrectly about your setup i’ll update my answer.