- Home /
Earning Money
So i have created a script where every 10 seconds you should earn 1$. But there is a problem with the script and i don't know what it is. any solutions?
Money Earning Script-------------------------------------------------------------
pragma strict
ar Earn = true;
function Start () {
}
function Update () {
if (Earn == true);
{
Player_Money.players_money += 1;
print(Player_Money.players_money);
Earn = false;
yield WaitForSeconds (10);
Earn = false; }
}
Player Money Script-----------------------------------------------------------------
static var players_money = 100;
function Start () {
}
function Update () {
}
please format your code as code. placing 4 spaces at the beginning of each line (remove all indents, and replace with 4 spaces)
What do you mean by 'there is a problem with the script'? Does it not work? Does Unity throw an error upon compilation?
you cant have yield WaitForSeconds in your Update function. also, you are setting earn to false before and after the yield call.. should probably be true after the yield
You only want money to be earned while the game is actually active right (not when it is paused, not running etc)?
Answer by Eric5h5 · Dec 27, 2012 at 05:41 PM
Use InvokeRepeating:
function Start () {
InvokeRepeating ("YoGimmeMoney", 10.0, 10.0);
}
function YoGimmeMoney () {
Player_Money.players_money++;
}
It should replace the current code of what you called $$anonymous$$oney Earning Script in your initial post.
Hmmm, I have just encountered a problem. somtimes is gives 2$ ins$$anonymous$$d of 1$. any solution?
Answer by KoningStoma · Dec 27, 2012 at 08:50 PM
I don't know why you use two separate scripts for this. I don't know exactly what you want to achieve, but I would do it something like this:
#pragma strict
var Earn = true;
var money = 0;
function Start () {
InvokeRepeating("GiveMoney",0,10);
}
function GiveMoney()
{
if(Earn == true) money+=1;
}
function Update () {
}
Your answer
Follow this Question
Related Questions
C# Saving data to the player's computer 1 Answer
Player Money not working 1 Answer
Saving changes while playing... 1 Answer
a one time script 1 Answer