Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by MatiasNaakka · Feb 28 at 10:27 AM · scripting problemgameobjectsshop

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!

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

279 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

My collectable objects don't respond like they should after calling a coroutine for the first time? 0 Answers

Creating a shopping cart in Unity scripts problems 0 Answers

Identifying 3 GameObjects in contact with each other,Identifying and destroying 3 Game Objects with the same LayerName 0 Answers

In Game Shop System (Use and Buy Problems) Help!!!!!!!! 0 Answers

Deleting random objects in started project 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges