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 /
  • Help Room /
avatar image
0
Question by ItzChris92 · Jun 27, 2017 at 09:13 PM · c#scripting problembeginnercurrency

Multiple in game currencies

I've written a very basic currency script, but I'm wondering how to make it work if I add other currency types. What I mean is, if I have droppable items for coins and for gems, with both of them calling AddCurrency() on pickup, how could I know which currency was picked up?

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.UI;
 
 public class CurrencyController : MonoBehaviour {
 
     public int coins;
     public int gems;
 
     public Text coinsDisplay;
 
     void Awake ()
     {
         UpdateCurrencyUI ();
     }
 
     void Update ()
     {
         AddCurrency (10);
     }
 
     void AddCurrency (int amount)
     {
         coins += amount;
         Debug.Log ("You now have " + coins.ToString() + " coins!");
         UpdateCurrencyUI ();
     }
 
     void RemoveCurrency (int amount)
     {
         coins -= amount;
         UpdateCurrencyUI ();
     }
 
     void UpdateCurrencyUI ()
     {
         coinsDisplay.text = coins.ToString();
     }
 }
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
2
Best Answer

Answer by nathanlink169 · Jun 27, 2017 at 10:08 PM

There are countless of ways to do it: The way that I would personally do it is to create an enum of currency types, and pass in that currency type into the function. For example:

 public enum eCurrencyTypes
 {
     Money,
     Gems,
 }

And

 public void AddCurrency(eCurrencyTypes in_currencyType, int in_amount)
 {
     m_Currency[in_currencyType] += in_amount;
 }

And

 private Dictionary<eCurrencyTypes, int> m_Currency = new Dictionary<eCurrencyTypes, int>();

Note that this code is not tested, so there may be some errors, but it should fundamentally work.

Comment
Add comment · Show 10 · 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
avatar image ItzChris92 · Jun 28, 2017 at 06:37 AM 0
Share

What is the dictionary for? Not used them before. Would this not work with just the enum of types?

avatar image nathanlink169 ItzChris92 · Jun 28, 2017 at 10:47 AM 0
Share

A Dictionary is just like an array, but ins$$anonymous$$d of passing in an integer to the array, you pass whatever the first value is. You can learn more about dictionaries here.

Essentially, by creating an enum, you're making it much easier to add currencies in the future: You simply need to add the currency type to the enum, and then make sure to initialize it in start.

avatar image ItzChris92 · Jun 28, 2017 at 08:33 PM 0
Share

I've played around with this.

 Debug.Log (eCurrencyTypes.Coins.ToString ());

This shows the string "Coins" in the console. So what is 'int in_amount' actually getting added to? Coins hasn't been referenced as an integer, so how can you add an integer to what appears to just be a string within an enum?

avatar image nathanlink169 ItzChris92 · Jun 28, 2017 at 08:42 PM 0
Share

An enum isn't a string, nor does it contain a string: the ToString method merely types out the name that you gave the enum option. You can read more on enums here.

Truth be told, enum's are really just glorified integers. If you hover your mouse over one of the options that the enum has, it will show you its integer value. It's simply a convenient way for you to write some code out.

As for "how you can add an integer to what appears to just be a string within an enum", take a look at the link to dictionaries that I posted above, that should explain that question.

avatar image ItzChris92 · Jun 28, 2017 at 10:41 PM 0
Share

Really appreciate these links and your input, thanks. So the initial int value of the enums will be 0, 1, 2 etc. right? Once the function AddCurrency is called, how do you then reference the enums new value to display as text? Would it be something like

 coinsDisplay.text = eCurrency.Coin

The examples in the videos and descriptions are slightly different from what I require, and I'm struggling to get my head around referencing the values of the enums themselves rather than their names

avatar image nathanlink169 ItzChris92 · Jun 29, 2017 at 10:46 AM 0
Share

No worries man: I wouldn't know this stuff if somebody didn't take the time to tell me!

Add another function inside your currency class:

 public float GetCurrency(eCurrencyType in_currencyType)
 {
     return m_Currency[in_currencyType];
 }

And from there, whenever you want to display it:

 coinsDisplay.text = CURRENCY_CONTROLLER_NA$$anonymous$$$$anonymous$$GetCurrency(eCurrencyType.Coin).ToString(); // this will display decimal places
 
 coinsDisplay.text = ((int)(CURRENCY_CONTROLLER_NA$$anonymous$$$$anonymous$$GetCurrency(eCurrencyType.Coin))).ToString(); // this will not display decimal places (because you are changing the type to an integer, which doesn't support decimal places.

You can learn more about typecasting (or changing from one type to another) here.

avatar image ItzChris92 · Jun 29, 2017 at 12:01 PM 0
Share

That makes much more sense now. I will try to put this together tonight.

Also, when I call AddCurrency (), it will take 2 arguments right? So something like

 AddCurrency (eCurrencyTypes.Coins, 10)
avatar image nathanlink169 ItzChris92 · Jun 29, 2017 at 12:38 PM 0
Share

That is correct

avatar image ItzChris92 · Jun 29, 2017 at 02:37 PM 0
Share

You are a god send :) Never touched dictionaries or enums, but now I have a base on which to learn and start using them efficiently! Thank you very much

avatar image nathanlink169 ItzChris92 · Jun 29, 2017 at 02:43 PM 0
Share

No worries man - like I said before, I wouldn't know this stuff if it wasn't for someone $$anonymous$$ching me.

Good learning!

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

395 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image 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

Integer multiplies itself by 3 for seemingly no reason 0 Answers

Error CS0120-Object Reference is required to access a non-static memberUnityEngine.Rigidbody.Velocity 1 Answer

How can i place subjects in different positions? 0 Answers

Accessing string from other script 1 Answer

Is there a way I can make one script for all my buttons in my game? 1 Answer


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