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
1
Question by Ben 18 · Nov 09, 2010 at 07:35 PM · javascriptarraystringoutofbounds

JavaScript String Question

Rather new to javascript, I am trying to create a two dimensional array and populate with a string that is it's space delineated location within the array. ie Array[2][2] would be "2 2". However I receive the compiler error BCE0024: The type 'String' does not have a visible constructor that matches the argument list '()'. What am I doing wrong?

var mapSizeX = 256; var mapSizeZ = 256;

function Awake () { mapArray = new Array(mapSizeX); for (var x = 1; x <= mapSizeX; x++) {
mapArray[x] = new Array(mapSizeZ); for (var z = 1; z <= mapSizeZ; z++) { mapArray[x][z] = new String (); mapArray[x][z] = x + " " + z; } } Debug.Log(mapArray[2][2]); }

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

2 Replies

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

Answer by skovacs1 · Nov 09, 2010 at 07:58 PM

  • Arrays are indexed starting at 0. If your code actually did compile, when you ran it, you would get an out-of-bounds error.
  • You don't need to call a String constructor (and you can't as per the error). When you assign x + " " + z, it creates a String for you because of your string concatenation. Because your array is non-typed, it would store non-string values if that's what you were actually passing in (you aren't), but if you were passing a non-string object to something expecting a String value, in most cases, it would implicitly call object.ToString() for you so you shouldn't have to worry about that anyways.

This will do what you want:

var mapSizeX = 256; var mapSizeZ = 256;

function Awake () { mapArray = new Array(mapSizeX); for (var x = 0; x < mapSizeX; x++) {
mapArray[x] = new Array(mapSizeZ); for (var z = 0; z < mapSizeZ;) { mapArray[x][z] = (x+1) + " " + (++z); } } Debug.Log(mapArray[1][1]); //0,0 stores 1,1. 1,1 stores 2,2. etc. }

But like Eric5h5 wisely points out, the c# multi-dimensional arrays are better anyways if they don't need to be resized.

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
Best Answer

Answer by Eric5h5 · Nov 09, 2010 at 08:00 PM

Strings are structs and do not need to be initialized with new, and in fact cannot be. Unless the arrays need to be dynamically sized, however, it would be quite a lot faster to use this for actual 2D string arrays instead of using Arrays of Arrays.

Comment
Add comment · Show 1 · 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 Ben 18 · Nov 09, 2010 at 08:09 PM 0
Share

I was trying to avoid using the work around with making a separate c script, but you are right that it's probably the best way. Thanks.

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

No one has followed this question yet.

Related Questions

Converting a .CSV string array to a float array 1 Answer

Javascript - Calling a random String 2 Answers

split() for separators more than 1 characters 2 Answers

convert string to array to write to document 0 Answers

Access a String array in one script from another script 0 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