how to change images by button onclick event in unity2d c#?

change the images based on button onclick event…
like if we clicked once one image have to display and clicked 2nd time another image have to display…
(using only one button)


  1. Pass the sprites in the array

  2. assign the image component

    internal class Imagechange


    [SerializeField] private Sprite[] _allSprites;
    [SerializeField] private Image _image;
    [SerializeField] private Button btn;
    public void ButtonClickListener()
        btn.onClick.AddListener(() =>
            for (var i = 0; i < _allSprites.Length; i++)
    /// <summary>
    ///     Replace main image with your sprites
    /// </summary>
    /// <param name="index">Index in the array</param>
    private void ReplaceImage(int index)
        _image.overrideSprite = _allSprites[index];