No changes on button click.

I am making a puzzle game where there are buttons generated through code and sprites are assigned randomly to the buttons… I created 2 different list of buttons on 2 different panels… the problem is that only panel 1 buttons seem to work properly…and panel 2 buttons are non interact-able!!
P.S :- This game is actually where if two images are same then they don’t flip around or else they go back to the original form.
If some one could help me it would be really great as i am a beginner in unity.
Thankyou!

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;


public class GameController : MonoBehaviour {


    [SerializeField]
    private Sprite bgImage;

    public Sprite[] puzzles1 , puzzles2;

    public List<Sprite> gamePuzzle1 = new List<Sprite>();
    public List<Sprite> gamePuzzle2 = new List<Sprite>();

    public List<Button> btns1 = new List<Button>();
    public List<Button> btns2 = new List<Button>();

    private bool firstGuess, secondGuess;

    private int countGuesses;
    private int countCorrectGuesses;
    private int gameGuesses;

    private int firstGuessIndex, secondGuessIndex;

    string firstGuessPuzzle, secondGuessPuzzle;

    void Awake()
    {
        puzzles1 = Resources.LoadAll<Sprite>("Sprites1");
        puzzles2 = Resources.LoadAll<Sprite>("Sprites1");
    }



void Start()
    {
        GetButtons();
        AddListeners();
        AddListener1();
       AddGamePuzzles();
        Shuffle1(gamePuzzle1);
        Shuffle2(gamePuzzle2);
        gameGuesses = (gamePuzzle1.Count + gamePuzzle2.Count) / 2;
    }

    void GetButtons()
    {
        GameObject[] objects1 = GameObject.FindGameObjectsWithTag("puzzlebutton");
        GameObject[] objects2 = GameObject.FindGameObjectsWithTag("puzzlebutton");

        for (int i = 0; i < objects1.Length; i++)
        {
            btns1.Add(objects1*.GetComponent<Button>());*

btns1*.image.sprite = bgImage;*
}

for (int i = 0; i < objects2.Length; i++)
{
btns2.Add(objects2*.GetComponent());*
btns2*.image.sprite = bgImage;*
}
}

void AddGamePuzzles ()
{
int looper1 = btns1.Count;
int index1 = 0;

for (int i = 0; i < looper1 ; i++)
{
if(index1 == looper1/2)
{
index1 = i;
}

gamePuzzle1.Add(puzzles1[index1]);
// gamePuzzle2.Add(puzzles2[index1]);

index1++;
}

int looper2 = btns2.Count;
int index2 = 5;

for (int i = 0; i < looper2; i++)
{
if (index2 == looper2/2)
{
index2 = i;
}

//gamePuzzle1.Add(puzzles1[index]);
gamePuzzle2.Add(puzzles2[index2]);

index2++;
}
}

void AddListeners()
{
foreach (Button btn1 in btns1)
{
btn1.onClick.AddListener(() => PickAPuzzle());

}
}

void AddListener1()
{

foreach (Button btn2 in btns2)
{
btn2.onClick.AddListener(() => PickAPuzzle());

}
}

public void PickAPuzzle()
{

if (!firstGuess)
{
firstGuess = true;

firstGuessIndex = int.Parse(UnityEngine.EventSystems.EventSystem.current.currentSelectedGameObject.name);
firstGuessPuzzle = gamePuzzle1[firstGuessIndex].name;
btns1[firstGuessIndex].image.sprite = gamePuzzle1[firstGuessIndex];
}
else if (!secondGuess)
{
secondGuess = true;

secondGuessIndex = int.Parse(UnityEngine.EventSystems.EventSystem.current.currentSelectedGameObject.name);
secondGuessPuzzle = gamePuzzle2[secondGuessIndex].name;
btns2[secondGuessIndex].image.sprite = gamePuzzle2[secondGuessIndex];

countGuesses++;

StartCoroutine(CheckIfThePuzzlesMatch());
}

}

IEnumerator CheckIfThePuzzlesMatch()
{
yield return new WaitForSeconds (1f);

if(firstGuessPuzzle == secondGuessPuzzle)
{
yield return new WaitForSeconds(.5f);
btns1[firstGuessIndex].interactable = true;
btns2[secondGuessIndex].interactable = true;

//btns[firstGuessIndex].image.color = new Color(0, 0, 0, 0);
//btns[secondGuessIndex].image.color = new Color(0, 0, 0, 0);

CheckIfTheGameIsFinished();
}
else
{
btns1[firstGuessIndex].image.sprite = bgImage;
btns2[secondGuessIndex].image.sprite = bgImage;
}

yield return new WaitForSeconds(.5f);
firstGuess = secondGuess = false;
}

void CheckIfTheGameIsFinished()
{
countCorrectGuesses++;

if(countCorrectGuesses == gameGuesses)
{
Debug.Log(“Game Finished”);
Debug.Log(“It Took you “+ countGuesses +” many gusses to finish the game”);
}
}

void Shuffle1(List list)
{
for (int i = 0; i < list.Count; i++)
{
Sprite temp = list*;*
int randomIndex = Random.Range(i, list.Count);
list = list[randomIndex];
list[randomIndex] = temp;
}
}

void Shuffle2(List list)
{
for (int i = 0; i < list.Count; i++)
{
Sprite temp = list*;*
int randomIndex = Random.Range(i, list.Count);
list = list[randomIndex];
list[randomIndex] = temp;
}
}
}

Hi! Apparently you get this code from YouTube lessons (Awesome Tuts) and tried something on your own. I don’t exactly know what you want to achieve but here are some comments concerning your code:

  • btn1 and btn2 are the same arrays cause objects1 and objects2 are the same arrays so when you call AddListeners() method you actually add two PickAPuzzle() functions (lamdas). Consequently when a button is pressed two PickAPuzzle() functions are called. So logic is wrong.

  • Why do you use btns1[firstGuessIndex].interactable = true; You should set interactable property to false instead

  • You haven’t provided a class which responsible for creating buttons so you should check it and set buttons’ interactable property to true.