Hello,
I am getting a little frustrated with my money system, it’s getting very complex and messy, so now I want to ask you guys, how would you organize this, and what methods would you use:
I have stacks of cash spread over my levels. The player can collect them to buy bonuses. There is a GUItext in the top right corner which displays the amount of money you have collected in the current level. After the player completes the level, a “profile” screen will appear. There the user can open a store, where total amount of money collected from all levels is displayed. Then the player can open a level select screen and play the next level, or play the previous ones again. If the user plays a once played level, there won’t be any money instantiated anymore. I also want to save the money (and the levels that have been unlocked, but I think that’s not a problem) to playerprefs, so that the user can use the money later.
This is what I would like to achieve. So far I have used this kind of method.
I have these parts in player control script to check for collision with money, and with goal:
if(CollisionInfo.gameObject.name == "Cash(Clone)")
{
gameObject.Find("MoneyGUI").SendMessage("MoneyCollected");
Destroy(CollisionInfo.gameObject);
}
if(CollisionInfo.gameObject.tag == "Finish")
{
gameObject.Find("MoneyGUI").SendMessage("OnGoal");
Application.LoadLevel("Profile Screen");
}
And there is a GUItext in every scene, which has this script attached to it. This receives the messages the player sends, and displays the money that has been collected. On moneycollect, it adds 100$ to the collectedMoney integer. On goal, it saves the collected money to playerprefs.
var collectedMoney : int = 0;
function OnGUI()
{
guiText.text = ("Money: "+collectedMoney+"$");
}
function MoneyCollected ()
{
collectedMoney += 100;
}
function OnGoal ()
{
PlayerPrefs.SetInt("Total Money", collectedMoney);
}
The money is instantiated with this script to all empty gameobjects with a tag “spawner”.
var cashPrefab : GameObject;
function Start()
{
var spawns = GameObject.FindGameObjectsWithTag ("Spawner");
for (var spawn in spawns)
Instantiate (cashPrefab, spawn.transform.position, spawn.transform.rotation);
}
In the profile menu, I use this snippet of script to display the total money:
function OnGUI()
{
guiText.text = ("You have: "+PlayerPrefs.GetInt("Total Money")+"$");
}
So, this is somewhat a working script, but not the way I want.
If I complete level 1 and collect 200$ and go to profile menu, it displays properly. However, if I then go to level 2, and collect 100$, my total money isn’t 300$, but 100$.
I think I should add 100 to playerprefs, instead of reading it always on goal. However, I read that I should only use PlayerPrefs.GetInt only when saving / loading. So I should probably re-write this whole stuff…
So, how could I organize this system better? Any ways to improve it? I’m not demanding you guys to write scripts for me, but a little help would be much appreciated.
Ps. This seems to be a very long and very confusing message, hope you understand what I mean.