I am creating an Idle game right now (tycoon style) which has 9 trains, (8 of them needed to be purchased), and 1 is always active, no matter if you just started the game.
I was trying to create a simple code, but it got really really messy, so there's 8 buttons, that each one, should "instantiate" the train and the trail of it, and after you press the first button (the most left one) the one next to it (in the right side) should be interactable, and when they press that button the train&mine on it should appear (right now I'm just using gameobject.active = true
), after they "purchased" the trail&train it should be always active (Using right now the playerprefs).
public class GameManager : MonoBehaviour
{
public Button[] Buttons;
public GameObject TrainB, TrainC, TrainD, TrainE, TrainF, TrainG, TrainH, TrainI;
public GameObject MineB, MineC, MineD, MineE, MineF, MineG, MineH, MineI;
public Text text;
public int CurrentTrainToSpawn = 0;
private void Start()
{
}
private void Update()
{
UpdateTrainPreFab();
}
public void ButtonA()
{
int ButtonAChoosed = PlayerPrefs.GetInt("PressedButtonA", 0);
if (ButtonAChoosed == 0)
{
Buttons[0].gameObject.active = false;
TrainB.gameObject.active = true;
MineB.gameObject.active = true;
PlayerPrefs.SetInt("PressedButtonA", 1);
}
}
public void ButtonB()
{
int ButtonBChoosed = PlayerPrefs.GetInt("PressedButtonB", 0);
if (ButtonBChoosed == 0 && PlayerPrefs.GetInt("PressedButtonA", 0) == 1)
{
Buttons[1].gameObject.active = false;
TrainC.gameObject.active = true;
MineC.gameObject.active = true;
PlayerPrefs.SetInt("PressedButtonB", 1);
}
}
public void ButtonC()
{
int ButtonCChoosed = PlayerPrefs.GetInt("PressedButtonC", 0);
if (ButtonCChoosed == 0 && PlayerPrefs.GetInt("PressedButtonB", 0) == 1)
{
Buttons[2].gameObject.active = false;
TrainD.gameObject.active = true;
MineD.gameObject.active = true;
PlayerPrefs.SetInt("PressedButtonC", 1);
}
}
public void ButtonD()
{
int ButtonDChoosed = PlayerPrefs.GetInt("PressedButtonD", 0);
if (ButtonDChoosed == 0 && PlayerPrefs.GetInt("PressedButtonC", 0) == 1)
{
Buttons[3].gameObject.active = false;
TrainE.gameObject.active = true;
MineE.gameObject.active = true;
PlayerPrefs.SetInt("PressedButtonD", 1);
}
}
public void ButtonE()
{
int ButtonEChoosed = PlayerPrefs.GetInt("PressedButtonE", 0);
if (ButtonEChoosed == 0 && PlayerPrefs.GetInt("PressedButtonD", 0) == 1)
{
Buttons[4].gameObject.active = false;
TrainF.gameObject.active = true;
MineF.gameObject.active = true;
PlayerPrefs.SetInt("PressedButtonE", 1);
}
}
public void ButtonF()
{
int ButtonFChoosed = PlayerPrefs.GetInt("PressedButtonF", 0);
if (ButtonFChoosed == 0 && PlayerPrefs.GetInt("PressedButtonE", 0) == 1)
{
Buttons[5].gameObject.active = false;
TrainG.gameObject.active = true;
MineG.gameObject.active = true;
PlayerPrefs.SetInt("PressedButtonF", 1);
}
}
public void ButtonG()
{
int ButtonGChoosed = PlayerPrefs.GetInt("PressedButtonG", 0);
if (ButtonGChoosed == 0 && PlayerPrefs.GetInt("PressedButtonF", 0) == 1)
{
Buttons[6].gameObject.active = false;
TrainH.gameObject.active = true;
MineH.gameObject.active = true;
PlayerPrefs.SetInt("PressedButtonG", 1);
}
}
public void ButtonH()
{
int ButtonHChoosed = PlayerPrefs.GetInt("PressedButtonH", 0);
if (ButtonHChoosed == 0 && PlayerPrefs.GetInt("PressedButtonG", 0) == 1)
{
Buttons[7].gameObject.active = false;
TrainI.gameObject.active = true;
MineI.gameObject.active = true;
PlayerPrefs.SetInt("PressedButtonH", 1);
}
}
public void UpdateTrainPreFab()
{
int ButtonAChoosed = PlayerPrefs.GetInt("PressedButtonA", 0);
if (ButtonAChoosed == 1)
{
Buttons[0].gameObject.active = false;
TrainB.gameObject.active = true;
MineB.gameObject.active = true;
PlayerPrefs.SetInt("PressedButtonA", 1);
}
int ButtonBChoosed = PlayerPrefs.GetInt("PressedButtonB", 0);
if (ButtonBChoosed == 1 && PlayerPrefs.GetInt("PressedButtonA", 0) == 1)
{
Buttons[1].gameObject.active = false;
TrainC.gameObject.active = true;
MineC.gameObject.active = true;
PlayerPrefs.SetInt("PressedButtonB", 1);
}
int ButtonCChoosed = PlayerPrefs.GetInt("PressedButtonC", 0);
if (ButtonCChoosed == 1 && PlayerPrefs.GetInt("PressedButtonB", 0) == 1)
{
Buttons[2].gameObject.active = false;
TrainD.gameObject.active = true;
MineD.gameObject.active = true;
PlayerPrefs.SetInt("PressedButtonC", 1);
}
int ButtonDChoosed = PlayerPrefs.GetInt("PressedButtonD", 0);
if (ButtonDChoosed == 1 && PlayerPrefs.GetInt("PressedButtonC", 0) == 1)
{
Buttons[3].gameObject.active = false;
TrainE.gameObject.active = true;
MineE.gameObject.active = true;
PlayerPrefs.SetInt("PressedButtonD", 1);
}
int ButtonEChoosed = PlayerPrefs.GetInt("PressedButtonE", 0);
if (ButtonEChoosed ==1 && PlayerPrefs.GetInt("PressedButtonD", 0) == 1)
{
Buttons[4].gameObject.active = false;
TrainF.gameObject.active = true;
MineF.gameObject.active = true;
PlayerPrefs.SetInt("PressedButtonE", 1);
}
int ButtonFChoosed = PlayerPrefs.GetInt("PressedButtonF", 0);
if (ButtonFChoosed == 1 && PlayerPrefs.GetInt("PressedButtonE", 0) == 1)
{
Buttons[5].gameObject.active = false;
TrainG.gameObject.active = true;
MineG.gameObject.active = true;
PlayerPrefs.SetInt("PressedButtonF", 1);
}
int ButtonGChoosed = PlayerPrefs.GetInt("PressedButtonG", 0);
if (ButtonGChoosed == 1 && PlayerPrefs.GetInt("PressedButtonF", 0) == 1)
{
Buttons[6].gameObject.active = false;
TrainH.gameObject.active = true;
MineH.gameObject.active = true;
PlayerPrefs.SetInt("PressedButtonG", 1);
}
int ButtonHChoosed = PlayerPrefs.GetInt("PressedButtonH", 0);
if (ButtonHChoosed == 1 && PlayerPrefs.GetInt("PressedButtonG", 0) == 1)
{
Buttons[7].gameObject.active = false;
TrainI.gameObject.active = true;
MineI.gameObject.active = true;
PlayerPrefs.SetInt("PressedButtonH", 1);
}
}
}