Trying to build Carshop, i need help with making list/array of the activating and deactivating car gameobjects.
Hi,
Im trying to build a CarShop. The idea how the CarShop should work is that player can click a purchase button inside the shop panel, if player has enough money. After that the purchased car gameobject should be activated and the previous car gameobject that was active before the new car purchase was made should be deactivated.
I have already made properly working BonusShop where player can buy different bonusses and used that script as a basis for this script, I have 6 car Gameobjects inside player prefab in unity editor hierarcy I guess i should use array or list but i dont know how to do this.
i can put the code i have already made here.
private Booster policeCar;
private Booster tractor;
private Booster fireTruck;
private Booster race;
private Booster deliveryTruck;
public GameObject policeCar1;
public GameObject tractor1;
public GameObject fireTruck1;
public GameObject race1;
public GameObject deliveryTruck1;
public GameObject money;
private UIController uiController;
private Text shopPanelMoneyValue;
private DBConnection database;
//private int CarId = 1;
void Start()
{
uiController = GameObject.Find("GameUI").GetComponent<UIController>();
database = new DBConnection();
//Car Prices
policeCar = new Booster(BoosterType.PoliceCar, 155);
tractor = new Booster(BoosterType.Tractor, 2000);
fireTruck = new Booster(BoosterType.FireTruck, 1000);
race = new Booster(BoosterType.Race, 1222);
deliveryTruck = new Booster(BoosterType.DeliveryTruck, 2032);
//Finding Car GameObjects
policeCar1 = GameObject.Find("police");
tractor1 = GameObject.Find("tractorShovel");
fireTruck1 = GameObject.Find("firetruck");
race1 = GameObject.Find("race");
deliveryTruck1 = GameObject.Find("delivery");
//List of Cars
List<GameObject> Cars = new List<GameObject>(5)
{
policeCar1, tractor1, fireTruck1, race1, deliveryTruck1
};
}
public class CarList
{
public void GetCarFromList(GameObject carType)
{
if (carType != null)
{
carType.SetActive(false);
}
bool isActive = carType.activeSelf;
carType.SetActive(!isActive);
}
}
void Update()
{
money.GetComponent<Text>().text = uiController.GetMoneyText();
policeCar.Update();
tractor.Update();
fireTruck.Update();
race.Update();
deliveryTruck.Update();
}
public void BuyBooster(string type)
{
switch (type)
{
case "PoliceCar":
policeCar.Buy();
break;
case "Tractor":
tractor.Buy();
break;
case "FireTruck":
fireTruck.Buy();
break;
case "Race":
race.Buy();
break;
case "DeliveryTruck":
deliveryTruck.Buy();
break;
default:
throw new ArgumentException("Unknown type: " + type);
}
}
// Booster sub class
public enum BoosterType
{
PoliceCar, Tractor, FireTruck, Race, DeliveryTruck
}
public class Booster
{
private BoosterType type;
private float price;
private Player player;
private Text priceText;
private Button button;
public Booster(BoosterType type, float initialPrice)
{
this.type = type;
this.price = initialPrice;
player = GameObject.Find("Player").GetComponent<Player>();
GameObject temp1 = GameObject.Find(type + "Price");
GameObject temp2 = GameObject.Find(type + "Booster");
if (temp1 != null)
{
priceText = GameObject.Find(type + "Price").GetComponent<Text>();
}
if (temp2 != null)
{
button = GameObject.Find(type + "Booster").GetComponent<Button>();
}
}
public void Update()
{
if (priceText != null)
{
priceText.text = price + " €"; // Setting starting price for item
}
if (button != null)
{
button.image.color = player.money >= price ? Color.yellow : Color.grey; // If player.money is less or equal to price purchase button is grey
}
priceText.text = price + " €"; // Setting starting price for item
button.image.color = player.money >= price ? Color.yellow : Color.grey; // If player.money is less or equal to price purchase button is grey
}
public void Buy()
{
if (player.money < price)
{
return;
}
bool bought = false;
switch (type)
{
case BoosterType.PoliceCar:
bought = true;
break;
case BoosterType.Tractor:
player.AddExperienceBonus(0.05f); // Update this one updated from player.AddMoneyBonus(0.05f);
bought = true;
break;
case BoosterType.FireTruck:
bought = true;
break;
case BoosterType.Race:
player.AddDeliveryTimeBonus(0.50f);
break;
case BoosterType.DeliveryTruck:
break;
default:
throw new ArgumentException("Unsupported type: " + type);
}
if (bought)
{
player.DecreasePlayerMoney(price);
}
Update();
}
public float GetPrice()
{
return price;
}
public void SetPrice(float value)
{
price = value;
}
}
}
Thank you!
Your answer
Follow this Question
Related Questions
Swapping characters breaks controls 0 Answers
Get All Game Objects Between 2 Points 1 Answer
How to create a child object inside the parent (script) and assign independent variables? 0 Answers
Reference problems to other Gameobjects 2 Answers
Same script activating separately on different game objects? 0 Answers