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 Sapidus3 · Oct 16, 2011 at 01:48 AM · javascriptarray

+= doesn't work with a 2d array in JS

In a JS I am using 2d arrays. The following works just fine

 infastructure[1,i]=infastructure[1,i]+desire[i]*popChange;

However, if I try this:

 infastructure[1,i]+=desire[i]*popChange;

I get the following error:

Internal compiler error. See the console log for more information. output was:BCE0051: Operator '+' cannot be used with a left hand side of type 'error' and a right hand side of type 'float'.

And the console does not even seem to be able to identify where in the code the error is arising. Throwing in a debug line before the offending line shows that infastructure[1,i] is properly declared by that point and has a float value in it and that I am not going out of bounds or anything like that.

I have tried a couple different ways of declaring the array including:

 var infastructure : float[,] = new float[8,10];

and

 var infastructure = new float[8,10];

But no dice.

I figure that the error has to do with JS's typing of variables or something equally annoying. Obviously this isn't a game stopper as I can just avoid using += statements, but it is annoying and right now I am a bit of a loss in regards to getting the darn thing to work.

Comment
Add comment · Show 2
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 jahroy · Oct 17, 2011 at 01:10 AM 0
Share

That is bizarre.

avatar image syclamoth · Oct 17, 2011 at 01:18 AM 0
Share

Try putting some brackets around the place, to see if you can make sure it does things in the right order.

1 Reply

· Add your reply
  • Sort: 
avatar image
-1

Answer by Flynn · Oct 16, 2011 at 05:43 AM

This is a truly strange one!

Before I go any further, it is important to note that the type of array you are using is not native to JS. It's actually a Unity3D tack-on! (From C# .NET, SEE: http://msdn.microsoft.com/en-us/library/2yd9wwz4.aspx ) So, right here you may be running into issues.

This stated, it may be possible that that type of array simply does not support the += operation (It's hard to say, I've tried to stay away from using it). You may want to try float[][], as opposed to float[,]

Assuming this is not true, it seems to be treating your + and your = as seperate operators.

Try: infastructure[1,i] += desire[i]*popChange; (note that the += is surrounded by spaces) To further specify to the compiler that you are NOT using + separately from the =, you could try (infastructure[1,i]) += (desire[i]*popChange); This isolates the += from either of your statements.

Honestly, I have never played with this type of array, so it is all speculation.

Other alternatives you could consider are:

 var arr = new Array();
 arr.push(new Array());

Now, this next one is tricky, and I have no idea if it will work. I've long dropped JS for C#, but theoretically it could work:

 var arr : ArrayList<float> = new ArrayList<float>;

You will want to import System.Collections for this to work!

Comment
Add comment · 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 Sapidus3 · Oct 17, 2011 at 01:01 AM 0
Share

Good suggestion but adding the spaces and parentheses did not help. I switched it over to a jagged array and it is all working fine. It is a bit disappointing though because I had heard that the newest version of unity actually supported real 2D arrays in Javascript. If I had realized this was still going to be an issue I would have just done it in C#.

I suppose I will probably submit a bug report unless someone else ends up posting some insight.

avatar image Eric5h5 · Oct 17, 2011 at 01:28 AM 0
Share

The array type isn't "tacked on" in JS, it's fully native. .NET and C# are two completely different things; all languages in Unity use .NET (actually $$anonymous$$ono, to be accurate). You almost never want to use the JS Array class or ArrayList. Regardless, you don't need to import System.Collections in JS. Whitespace is irrelevant to the compiler (its only use is to make things more understandable to humans), and adding parentheses to "isolate" things like that is also irrelevant.

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

6 People are following this question.

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

Related Questions

Is there a decent tutorial for a local high score table (android)? 1 Answer

Error when debuging array elements name 1 Answer

Sorting arrays? 1 Answer

How to delete new'd arrays? 2 Answers

How to set parents of all objects in an array? 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