- Home /
Trouble making money system
I am coding a money system in JavaScript, and am making a function that adds money to the total amount. But I get two errors:
Assets/Money.js(5,9): BCE0044: expecting }, found 'public'.
Assets/Money.js(11,1): BCE0044: expecting EOF, found '}'.
Here is my code:
#pragma strict
function AddMoney(MoneyToAdd)
{
public var Money : GameObject;
public var MoneyAmount : float;
public var NewMoneyAmount : float;
NewMoneyAmount = MoneyAmount + MoneyToAdd;
Money.guiText.text = NewMoneyAmount;
}
Edit: Ok, I changed it a bit, but now get the error: Assets/Money.js(17,19): BCE0043: Unexpected token: 5.0.
Here is the new code: #pragma strict
class ChangeMoney{
public var Money : GameObject;
public var MoneyAmount : float;
public var NewMoneyAmount : float;
function AddMoney(MoneyToAdd : float)
{
NewMoneyAmount = MoneyAmount + MoneyToAdd;
Money.guiText.text = "£" && NewMoneyAmount;
}
}
function AddMoney(5.0)
Please could I have an answer for both bits of code.
Answer by developer_arni · Oct 25, 2013 at 08:09 PM
In you first piece of code, you are trying to define if you local variables should be either public or private, but inside a body, ie "{ ... }" the functions are local to that body, you might say private. All variables created inside a body, also 'die' when reaching the end of a body, ie a "}".
The keywords public and private only makes sense when defining class variables, as they determine whether other instances can access these variables directly.
The other piece of code:
You cannot define a function like this:
function AddMoney(5.0)
When defining a parameter, you must define its type and some name, that you define, some language allow you to also define a default value for a parameter, but not in unity-javascript, therefore you must do it like you do previously:
function AddMoney(value : float)
If you want to let AddMoney take a default value of 5.0f, then you cannot do this in unity-javascript, however you can do this in unity-c#, although it cannot be called from a javascript file, and therefore to utilize this functionality, you must write your whole project in c#, or create some extra wrapper functions in c# which can be called from javascript, nonetheless; messy.
I am sorry, but I still do not understand the second one. What do I write for the function?
Let me start by asking; what is it you want to achieve with that line/function? You have already defined the function Add$$anonymous$$oney above.