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.
so you want someone to develop a money system tailored to your needs for you for free? here you go: Code (csharp): public class MoneySystem { public int dollars; { seriously, i don't get your point. whats your question?
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...
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.
Hello! Here's a simple example of a money system: Code (csharp): using UnityEngine; using System.Collections; public class MoneySystem : MonoBehaviour { //an internal reference to the system itself private static MoneySystem _instance; //current balance public int money; //interval for saving the money to Playerprefs public float saveInterval; //internal variable which uses getters and setters to ensure that the money system is ALWAYS available. private static MoneySystem instance { get { //if the instance is null, first make sure there's not already a gameobject named MoneySystem. If there is, check for the //MoneySystem component and set it as instance, otherwise add the component and set the new one as instance. // If there isn't a gameobject named MoneySystem, make one and add the MoneySystem component. //Lastly, return the instance. if (_instance == null) { if (GameObject.Find("MoneySystem")) { GameObject g = GameObject.Find("MoneySystem"); if (g.GetComponent<MoneySystem>()) { _instance = g.GetComponent<MoneySystem>(); } else { _instance = g.AddComponent<MoneySystem>(); } } else { GameObject g = new GameObject(); g.name = "MoneySystem"; _instance = g.AddComponent<MoneySystem>(); } } return _instance; } set { _instance = value; } } void Start() { //Make sure the Gameobject is named MoneySystem. gameObject.name = "MoneySystem"; _instance = this; //load the saved money AddMoney(PlayerPrefs.GetInt("MoneySave", 0)); //start the save interval. StartCoroutine("SaveMoney"); } //while reality exists, save money every saveInterval. public IEnumerator SaveMoney() { while (true) { yield return new WaitForSeconds(saveInterval); PlayerPrefs.SetInt("MoneySave", instance.money); } } //Checks if you have enough money to buy item with cost, if you do buy it and return true. Otherwise, return false. public static bool BuyItem(int cost) { if (instance.money - cost >= 0) { instance.money -= cost; return true; } else { return false; } } //Simply return the balance public static int GetMoney() { return instance.money; } //Add some money to the balance. public static void AddMoney(int amount) { instance.money += amount; } } 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 !
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? ^^
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
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...
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
I know it's been a long time, but i hope someone can help me, i might be dumb but i have no idea how i would add money will calling the function because what you right does not seems t oexist even if i made the MoneySystem script
classes are the same as objects in the real world, you cant tell unless you ask me if i have a glass, whats in it, and how much. A class will work the same, you cant tell if any given object has a money system, or not, unless you go look for it. If this is beyond you, i'd suggest following some basic unity tutorials, as your classes in essence require the the same logic to be found and used as any other unity ones do, the only difference is it runs your code not theirs. So, if this is beyond you, then you need to go back to basics