hi im jarrett and i tried making a javascript for a bankaccount in my game. and once i go through the print procedure and save it and drag it onto my floor nothing happens. i tried dragging it onto everything but it wont print on the screen. what do i do wrong if i want to subtract 100 dollars…
var bankAccount = 1000;
bankAccount = bankAccount-100;
print(bankAccount);
What did i do wrong?
For starters, you question is really vague. With that said, I’m going to assume you want to be able to display the total amount in your bank account on screen. Also, the print() function only outputs to your console window and not your screen. Here’s a quick script to help you get up and running:
First off, create an empty game object and put it into your world and call it “bankmanager”. Once you’ve created this empty game object, you are going to drag the script I wrote below onto that game object in order for it to display.
script name: “bankMgr.js”
// Start Script -------->
private var bankAccount:int;
function Start()
{
// Set initial amount in bank
SetBalance(1000);
}
function OnGUI()
{
// Display Bank Account Balance
GUI.Label(Rect(10,10,200,30),"Balance: " + GetBalance.toString());
// optional GUI buttons to modify the balance for demo purposes
if(GUI.Button(Rect(10,50,200,50),"Deposit $100")) { Deposit(100); }
if(GUI.Button(Rect(10,110,200,50),"Withdrawal $100")) { Withdrawal(100); }
}
function Deposit(amount:int):void { bankAccount += amount; }
function Withdrawal(amount:int):void { bankAccount -= amount; }
function GetBalance():int { return bankAccount; }
function SetBalance(amount:int):void { bankAccount = amount; }
// End Script -------->
The above script is just a quick way of getting / setting and managing a bank account and having its balance display on screen. Good Luck!