Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
avatar image
0
Question by lynxcs · Jan 22, 2017 at 09:44 PM · instantiationmodularelements

How to make a modular buying system?

Hi,

I'm making a basic 2D clicker game and I have ran into a problem.

I want my "buying" system to be modular.

What I mean by this is that in the inspector, I can specify how many elements there are, set the values of those elements individually (e.g one's "cost" is set to 1 and another to 5) and make the buttons appear (with those names, values etc.) a set distance between one another.

I have tried class arrays but then when I try to make them instantiate I get lots of errors.

Thanks in advance.

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by FlyingHighUp · Jan 23, 2017 at 12:29 AM

Having written one myself, start from a general "Product" class, and work from there.

For instance:

 public abstract class Product : MonoBehaviour 
 {
     public string Name = "Product";
     public string Description = "No Description";
     public Texture PreviewImage;
     /// <summary>
     /// Attempts to buy the product. Returns true if it was successful, returns false with a error message if not.
     /// </summary>
     /// <param name="callback">Callback.</param>
     public abstract bool TryBuy(out string error);
 
     /// <summary> Returns the price string of a product. e.g. "100 Coins" </summary>
     /// <returns>The cost string.</returns>
     public abstract string GetCostString();
 
     public virtual bool IsAlreadyPurchased()
     {
         return false;
     }
 
     public virtual bool Purchaseable()
     {
         return true;
     }
 }

Then extend it for each product you want, e.g.

 public class ShinyThing : Product
 {
     public int Cost = 10;
 
     //Class that loads/saves player coins to a file.
     PlayerFundsHelper fundsHelper;
 
     void Awake()
     {
         fundsHelper = new PlayerFundsHelper();
     }
 
     public override bool TryBuy(out string error)
     {
         Funds playerFunds = fundsHelper.GetFunds();
         if (playerFunds.Coins < Cost)
         {
             int missingCoins = Cost - playerFunds.Coins;
             error =  "Not enough funds. You're missing " + missingCoins + " :("));
             return false;
         }
 
         playerFunds.Coins -= Cost;
 
         //Save the results
         fundsHelper.SetNewFunds(playerFunds);

             //!!!Insert code here to add shinyThing to your player's inventory!!!
             
         return true;
     }
 
     public override string GetCostString()
     {
         return Cost + " Coins";
     }
 
 }

Finally, write a class that finds all the product classes you created in the scene and add it to a list. This one works by looking for Product Monobehaviours on children gameobjects.

 public class Store : MonoBehaviour 
 {
     List<Product> products;
     public List<Product> Products { get { return products; } }
 
     // Use this for initialization
     void Start () 
     {
         initialize();
     }
 
     void initialize()
     {
         //Now search for products, and add them to the products list.
         products = new List<Product>();
         products.AddRange(this.GetComponentsInChildren<Product>());
         products.AddRange(this.GetComponents<Product>());
     }
 
 
 }

Then to buy something...

 Store store = FindObjectOfType<Store>();
 //Attempt to buy the first product in the store
 string error;
 if (! store.Products[0].TryBuy(out error))
 {
       Debug.LogError("Purchase failed: " + error);
 }
 else
 {
             Debug.LogError("Yay the product was bought");
 }

Of course, all of this will change depending on the nature of your game.

To create your list of UI Buttons. Have another class that iterates over the products and generates a UI prefab in a scrollview. Then pass each product to a class on that prefab, and make it so pressing the button purchases that product.

Comment
Add comment · Share
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

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Using Transform on instantiated objects 1 Answer

Simple bullet instantiation problem 1 Answer

Awake() not called on script referenced directly from Project window 1 Answer

GameObject instantiation in C# static constructor 2 Answers

Shoot 'Em Up: Best way to instantiate enemies 2 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