Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

money system using c#?

Discussion in 'Getting Started' started by oc007061, Jan 22, 2014.

  1. oc007061

    oc007061

    Joined:
    May 27, 2013
    Posts:
    10
    Ok, i bet this has been asked bunches of times, and i did find this, but i dont want cents like it describes there, only dollars. Much like in GTA (Grand Theft Auto) is what i want.I just dont know how to make it only dollars. The script at the link looks like what i could use, but it has cents. I request c# as javasript looks wonky to me.
     
  2. exiguous

    exiguous

    Joined:
    Nov 21, 2010
    Posts:
    1,749
    so you want someone to develop a money system tailored to your needs for you for free? here you go:
    Code (csharp):
    1.  
    2. public class MoneySystem
    3. {
    4.     public int dollars;
    5. {
    6.  
    seriously, i don't get your point. whats your question?
     
    blizzy and Kiwasi like this.
  3. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    just in case you didn't notice the difference between the linked thread and exiguous' post: floats are decimal values, ints are whole numbers only...
     
  4. Glader

    Glader

    Joined:
    Aug 19, 2013
    Posts:
    449
    You're looking for the most primitive of datatypes.

    By that I mean the first one that is usually learned about. The integer! Might I recommend finding a programming book to read the first few chapters.
     
  5. AngryGenius

    AngryGenius

    Joined:
    Jan 21, 2014
    Posts:
    14
    Hello! Here's a simple example of a money system:

    Code (csharp):
    1.  
    2.  
    3. using UnityEngine;
    4. using System.Collections;
    5.  
    6. public class MoneySystem : MonoBehaviour {
    7. //an internal reference to the system itself
    8.     private static MoneySystem _instance;
    9.  
    10. //current balance
    11.     public int money;
    12.  
    13. //interval for saving the money to Playerprefs
    14.     public float saveInterval;
    15.  
    16. //internal variable which uses getters and setters to ensure that the money system is ALWAYS available.
    17.     private static MoneySystem instance
    18.     {
    19.         get
    20.         {
    21. //if the instance is null, first make sure there's not already a gameobject named MoneySystem. If there is, check for the
    22. //MoneySystem component and set it as instance, otherwise add the component and set the new one as instance.
    23. // If there isn't a gameobject named MoneySystem, make one and add the MoneySystem component.
    24. //Lastly, return the instance.
    25.             if (_instance == null)
    26.             {
    27.                 if (GameObject.Find("MoneySystem"))
    28.                 {
    29.                     GameObject g = GameObject.Find("MoneySystem");
    30.                     if (g.GetComponent<MoneySystem>())
    31.                     {
    32.                         _instance = g.GetComponent<MoneySystem>();
    33.                     }
    34.                     else
    35.                     {
    36.                         _instance = g.AddComponent<MoneySystem>();
    37.                     }
    38.                 }
    39.                 else
    40.                 {
    41.                     GameObject g = new GameObject();
    42.                     g.name = "MoneySystem";
    43.                     _instance = g.AddComponent<MoneySystem>();
    44.                 }
    45.             }
    46.  
    47.             return _instance;
    48.         }
    49.  
    50.  
    51.         set
    52.         {
    53.             _instance = value;
    54.         }
    55.     }
    56.  
    57.     void Start()
    58.     {
    59. //Make sure the Gameobject is named MoneySystem.
    60.         gameObject.name = "MoneySystem";
    61.  
    62.         _instance = this;
    63.  
    64. //load the saved money
    65.         AddMoney(PlayerPrefs.GetInt("MoneySave", 0));
    66.  
    67. //start the save interval.
    68.         StartCoroutine("SaveMoney");
    69.     }
    70.  
    71. //while reality exists, save money every saveInterval.
    72.     public IEnumerator SaveMoney()
    73.     {
    74.         while (true)
    75.         {
    76.             yield return new WaitForSeconds(saveInterval);
    77.             PlayerPrefs.SetInt("MoneySave", instance.money);
    78.         }
    79.     }
    80.  
    81. //Checks if you have enough money to buy item with cost, if you do buy it and return true. Otherwise, return false.
    82.     public static bool BuyItem(int cost)
    83.     {
    84.         if (instance.money - cost >= 0)
    85.         {
    86.             instance.money -= cost;
    87.             return true;
    88.         }
    89.         else
    90.         {
    91.             return false;
    92.         }
    93.     }
    94.  
    95. //Simply return the balance
    96.     public static int GetMoney()
    97.     {
    98.         return instance.money;
    99.     }
    100.  
    101. //Add some money to the balance.
    102.     public static void AddMoney(int amount)
    103.     {
    104.         instance.money += amount;
    105.     }
    106. }
    107.  
    108.  
    It's a singleton class, which means that you can always call the three methods on it. It also saves every 15 seconds, which is set by the variable "saveInterval" at the top.
    You can buy things by calling the MoneySystem.BuyItem(int cost) method, which will return true if the transaction is successful or false if not.
    You can check the current balance by using the MoneySystem.GetMoney() method, which returns the balance.
    And finally you can add money with MoneySystem.AddMoney(int amount).

    I hope I was helpful :D!
     
    scorgon48, Maxzzzz and dmitry-K like this.
  6. oc007061

    oc007061

    Joined:
    May 27, 2013
    Posts:
    10
    Works pretty good, thanks! Kinda curious, how do I add money when walking into the collider of another object? Do I add the script on that object and make it OnTriggerEnter? ^^
     
  7. paulus51

    paulus51

    Joined:
    Dec 5, 2012
    Posts:
    33
    i added this script to a empty gameobject renamed it to MoneySystem but i cant add this script to it , unity say :



    i checked the file ( c#sharp monodevelop) copyed the name and past it ti save so whaat is wrong here ?
    reimported again and again , so please need a littel hepl here
     
  8. Willy_2202

    Willy_2202

    Joined:
    Feb 19, 2015
    Posts:
    50
    You need to make sure the script is named "MoneySystem"
     
  9. Willy_2202

    Willy_2202

    Joined:
    Feb 19, 2015
    Posts:
    50
    H
    ey can you please make it to where a GUI text is in the corner of the screen, I dont know about coding that much, and I kinda know how to make the GUI, I just don't know where to insert it into the script...
     
  10. Willy_2202

    Willy_2202

    Joined:
    Feb 19, 2015
    Posts:
    50
    Can you kinda give an example script of Buying something...
     
  11. rakkarage

    rakkarage

    Joined:
    Feb 3, 2014
    Posts:
    683
    The decimal keyword indicates a 128-bit data type. Compared to floating-point types, the decimal type has more precision and a smaller range, which makes it appropriate for financial and monetary calculations. The approximate range and precision for the decimal type are shown in the following table.

    https://msdn.microsoft.com/en-us/library/364x0z75.aspx