- Home /
Other
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[i].GetComponent<Button>());
btns1[i].image.sprite = bgImage;
}
for (int i = 0; i < objects2.Length; i++)
{
btns2.Add(objects2[i].GetComponent<Button>());
btns2[i].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<Sprite> list)
{
for (int i = 0; i < list.Count; i++)
{
Sprite temp = list[i];
int randomIndex = Random.Range(i, list.Count);
list[i] = list[randomIndex];
list[randomIndex] = temp;
}
}
void Shuffle2(List<Sprite> list)
{
for (int i = 0; i < list.Count; i++)
{
Sprite temp = list[i];
int randomIndex = Random.Range(i, list.Count);
list[i] = list[randomIndex];
list[randomIndex] = temp;
}
}
}
Answer by Atmor85 · Mar 13, 2018 at 04:48 PM
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
andbtn2
are the same arrays causeobjects1
andobjects2
are the same arrays so when you callAddListeners()
method you actually add twoPickAPuzzle()
functions (lamdas). Consequently when a button is pressed twoPickAPuzzle()
functions are called. So logic is wrong.Why do you use
btns1[firstGuessIndex].interactable = true;
You should set interactable property tofalse
insteadYou haven't provided a class which responsible for creating buttons so you should check it and set buttons' interactable property to
true
.
Yes i have got this code from a you tube tutorial(Awesome tuts)... the thing i want to achieve here is that whenever the puzzle is loaded the five sprite that are loaded on to the buttons, they should be in a proper format ... As in 5 sprites should appear in a single column without any repetition and the same thing goes with the lower column. I Hope you understand my problem and what I am trying to do!! P.S :- Yes i have already created a class for creating buttons. Thank you! @Atmor85 if you could help me out with this :)
So I think you should stick to the tutorial and you'll get a working code :)
Yes i did ..but things don't seem to work in the intended way!! Anyways thank you :) @Atmor85
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Assigning a Sprite to a Script and creating a Gameobject out of that sprite 1 Answer
How do I make a custom font for a GUI button? C# 1 Answer
How to change the sprite and the animator in script to make a character selection? 2 Answers