I have a strange problem (at least for me) that I do not understand with my bool “firstClick”. I know I have it declared and used in only one single script. Part of my debugging was to rename it.
The problem is that it is triggered differently if I start the script from the actual script or from another class despite the fact that both start sequences include init it as true.
When I initiate from GameManager class I get the following result (print):
InitializeGame - firstClick: True
BTN_Selected - firstClick: True <<<<
A) C
BTN_Selected - firstClick: False
pnl_MenuNoAccess.SetActive(true)
B) A
pnl_MenuNoAccess.SetActive(false)
…which is exactly as I want it.
However, when I start the initiation from SetupMatch I get a different result:
InitializeGame - firstClick: True
BTN_Selected - firstClick: False <<<<
pnl_MenuNoAccess.SetActive(true)
B) A
BTN_Selected - firstClick: True
A) C
pnl_MenuNoAccess.SetActive(false)
I just do not understand why firstClick becomes false as it is init as true in the GameManager Start function. The SetupMatch class inherit from GameManager, which is the main class. I guess it must have something to do with that but I do not understand why.
Here is the “main” script:
public class GameManager : MonoBehaviour
private bool firstClick;
private void Start()
{
Utilities._ui = FindObjectOfType(typeof(UI_Alloc_and_More)) as UI_Alloc_and_More;
_sm = FindObjectOfType(typeof(SetupMatch)) as SetupMatch;
Utilities.AllocateUI();
Utilities.SetColorScheme();
Utilities.AssigningValuesToButtons(0);
InitializeGame(); <<<<<<<<<<<<
}
public void InitializeGame()
{
Utilities.AssigningValuesToButtons(1);
Utilities.AssignButtons(20); // Between 4 - 20
firstClick = true;
print("firstClick: " + firstClick);
isMovingCardsToTrashCan = false;
moveSpeed = 4;
points = 0;
Utilities.txt_Points.text = points.ToString();
}
Here is the buttons:
public void BTN_Selected(int _btnNr)
{
print("BTN_Selected - firstClick: " + firstClick);
if (firstClick)
{
int_Btn_A_Nr = _btnNr;
btn_A = btn_List[int_Btn_A_Nr].name;
firstClick = false;
print("A) " + btn_A);
// Flip from backside to front
btn_Temp = btn_List[int_Btn_A_Nr];
btn_Temp.image.sprite = Utilities._ui.spr_myFront;
btn_Temp.image.color = Utilities.colorCardFrontUI;
btn_Temp.GetComponentInChildren<Text>().color = Utilities.colorCardFrontText;
btn_Temp.GetComponentInChildren<Text>().text = btn_Temp.name;
}
else
{
// Inhibit access during the card move-out
pnl_MenuNoAccess.SetActive(true);
print("pnl_MenuNoAccess.SetActive(true)");
int_Btn_B_Nr = _btnNr;
btn_B = btn_List[int_Btn_B_Nr].name;
firstClick = true;
print("B) " + btn_B);
// Flip from backside to front
btn_Temp = btn_List[int_Btn_B_Nr];
btn_Temp.image.sprite = Utilities._ui.spr_myFront;
btn_Temp.image.color = Utilities.colorCardFrontUI;
btn_Temp.GetComponentInChildren<Text>().color = Utilities.colorCardFrontText;
btn_Temp.GetComponentInChildren<Text>().text = btn_Temp.name;
if (btn_A == btn_B)
{
print("btn_A == btn_B");
// This is a hit: Wait a few seconds and then add points and move the cards out the scene and destroy them.
points++;
Utilities.txt_Points.text = points.ToString();
// Move card to trashcan
Invoke(nameof(MoveHitCardsToCardTrashCan), 1.5f);
}
else
{
Invoke(nameof(FlipTheCard), 1.5f);
// Enable access during the card move-out
}
btn_A = "";
btn_B = "";
}
}
Another class:
public class SetupMatch : GameManager
public void BTN_PlayGame()
{
InitializeGame();
}