• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
Question by ashjack · Oct 25, 2013 at 07:27 PM · javascripterrorfunctionfloatmoney

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:

  1. Assets/Money.js(5,9): BCE0044: expecting }, found 'public'.

  2. 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.

Comment

People who like this

0 Show 0
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image

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.

Comment

People who like this

0 Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image ashjack · Oct 25, 2013 at 09:16 PM 0
Share

I am sorry, but I still do not understand the second one. What do I write for the function?

avatar image developer_arni · Oct 26, 2013 at 11:33 AM 0
Share

Let me start by asking; what is it you want to achieve with that line/function? You have already defined the function AddMoney above.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

16 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Script is causing immense lag, and I don't know what's causing it. 2 Answers

Similar scripts but error on one and not the other 1 Answer

[SOLVED] Help with PlayerPrefs.SetInt 1 Answer

Assets/Editor/UpdateTreeColors.js(13,17): BCE0031: Language feature not implemented: UnityEditor. 1 Answer

Trying to send message with 2 variables, but getting error. 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges