Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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
0
Question by Nathaniel97 · Aug 24, 2012 at 01:37 PM · errornot workingjava

Why isn't my java code working?

I'm quite new to java so this may sound abit dim. I've been doing some video tutorials for unity this is the code that I have writen:

 var box;  
 box = "small box";  
 print(box);  
 var boxNumber;  
 boxNumber = 23;  
 print(boxNumber);  
 var boxTotal;  
 boxTotal = boxNumber + box;  
 print(boxTotal);  

It is supposed to display the words small box + the box number but is keeps giving me this error: Assets/scripting basics.js(31,30): BCE0051: Operator '+' cannot be used with a left hand side of type 'Object' and a right hand side of type 'Object'. What does the error meen cause im quite puzzled by it.

Any help would be much appreciated, thankyou very much.

Comment
Add comment · Show 1
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 Eric5h5 · Aug 27, 2012 at 07:23 PM 0
Share

Unity does not use Java, just so you know.

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Bunny83 · Aug 24, 2012 at 03:47 PM

Like Semicolon said you should give your variables a type. Mono / .Net is a compiled and type safe language. When you don't provide a type, it's type will be a dynamic "Object". Dynamically typed variables are slow since the runtime has to check the type when you use the variable. Also, like the problem you have here, since the type is not known at compile time you can't use operators like + on that type since it could be a string, a class, null, or whatever.

Next thing is you try to add a string to a number. That doesn't work unless you convert the string into a number. However your string doesn't contain a numer, so do you want to concat the numer as string with the other string?

 var box : String;
 box = "small box";
 print(box);
 var boxNumber : int;
 boxNumber = 23;
 print(boxNumber);
 var boxTotal : String;
 boxTotal = boxNumber.ToString() + box;
 print(boxTotal); // Should print "23small box"

A final note: Java is a completely different language as JavaScript. Additionally the "JavaScript" variant that is used in Untiy is also quite different from traditional JavaScript. That's why it's actually called UnityScript.

Comment
Add comment · 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
1

Answer by Semicolon · Aug 24, 2012 at 03:22 PM

That means that it hasn't been instructed on how to add 2 Objects.

Try using "var :String;" instead. So:

 var box:String;
Comment
Add comment · 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

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.

Follow this Question

Answers Answers and Comments

10 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

Related Questions

SCRIPT NOT WORKING 2 Answers

Animation script error 1 Answer

Console Error 1 Answer

OnMouseEnter Error 1 Answer

How do i fix this problem in my code? please help 2 Answers


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