Show and Hide panel with random roll
So I am fairly new to coding in general. I am messing around with a project trying to make it so whenever I press a button it shows 1 out of the 4 panels. Right now the way I have it setup once one of the panels pops up it stays on forever and it only shows the first panel that popped up. I want to be able to make it so when I click the button it hides all 4 panels then shows the one that got picked from the random roll.
Currently this is my code
public GameObject fireCard;
public GameObject waterCard;
public GameObject holyCard;
public GameObject forestCard;
int troopNumber;
public void ClickTheButton()
{
int troopNumber = Random.Range(1, 4);
if (troopNumber == 1)
{
fireCard.SetActive(true);
}
if (troopNumber == 2)
{
waterCard.SetActive(true);
}
if (troopNumber == 3)
{
holyCard.SetActive(true);
}
if (troopNumber == 4)
{
forestCard.SetActive(true);
}
}
I've tried making it so it constantly only picks one and adding the code below, but nothing happens when I click the button.
if (troopNumber == 1) { fireCard.SetActive(false); fireCard.SetActive(true); }
Answer by Nedidark · May 10, 2020 at 01:17 PM
So if anyone has the same problem here is the fix I ended up with.
I added a boolean, bool CardShowing = false; I made another void
void HideCard() { fireCard.SetActive(false); waterCard.SetActive(false); forestCard.SetActive(false); holyCard.SetActive(false); CardShowing = false; }
And in the public void each roll number has this base
if (troopNumber == 1) { if (CardShowing) { HideCard(); } fireCard.SetActive(true); Debug.Log("Showing Fire Card"); CardShowing = !CardShowing; }
If someone knows a way to make it so I can set all of my cards inactive with 1 line that'd be cool to learn. For now I have it so it does it one by one.
Your answer
Follow this Question
Related Questions
How to change a game objects tag after it has been randomly selected? 0 Answers
changing the color of a random game object not working 0 Answers
Game Over panel do not setactive true when collide with enemy, Why ? Please help! 0 Answers
LevelGenerator after Checkpoint Respawn Not Aligning Tiles Properly 1 Answer
How do I check for random range which is divisible by 2 2 Answers