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 Flame of Endil · Mar 25, 2013 at 01:46 AM · arraytypelengthbuiltin

Call array-specific methods on arrays within an array?

I have a function that requires input in the form of an array, but I want to be able to specify which array is passed to the function. I have initialized several arrays (builtin, if that's relevant) and stored them inside another array (JS Array).

Here is a simplified example of what I've got:

 var numbers = new String[3];
     numbers[0] = "1";
     numbers[1] = "2";
     numbers[2] = "3";
 
 var letters = new String[3];
     letters[0] = "1";
     letters[1] = "2";
     letters[2] = "3";
     
 var names : Array = new Array(
     numbers,
     letters
     );
     
 function Evaluate(input) {
     for (var each in input) {
         print (each);
     }
 }
 
 
 function Start () {
     Evaluate(names[1]);
     print(names[0].Length);
 }

When I run the script, the Evaluate function works as expected. However, the print statement throws the following error: "Assets/NewBehaviourScript.js(27,24): BCE0019: 'Length' is not a member of 'Object'."

I'm guessing that I have to let Unity know that the contents of array "names" are all of type "builtin array", and that they DO support to .Length method. I've gotta supply the type somehow. Where? How?

Any explanation would be very helpful, because I've been trying to figure this out forever... with absolutely no success.

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 Benproductions1 · Mar 25, 2013 at 01:49 AM 0
Share

uncapitalize Length and you should be fine

avatar image Flame of Endil · Mar 25, 2013 at 02:32 AM 0
Share

Good thought! Unfortunately, I had already tried both capitalizations, receiving the same result regardless. ($$anonymous$$ore detailed comment below) Any other possibilities?

1 Reply

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

Answer by Loius · Mar 25, 2013 at 03:29 AM

Array is a terrible class. Don't use it unless you really really gotta have that type indifference.

Array can only store Object references. Any time you want to use an element of an Array, you need to cast it: var interiorArray : Array = exteriorArray[0] as Array

array[0].Length doesn't work because array[0] is an Object, not an Array.

Use List to get around every problem with Array. It has all the same functionality and more besides, and it operates quite a bit faster and you never need to cast out of it. The only thing you need to do to change from Array to List is:

var myArray : Array = new Array();

becomes

var myList : List.< Type > = new List.< Type >();

Comment
Add comment · Show 10 · 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 Eric5h5 · Mar 25, 2013 at 03:43 AM 0
Share

Actually you shouldn't use ArrayList either, it's not really any better than Array and has the same issues. I assume you mean List? (Also, there's really no reason to use Array ever...if you need an untyped array you can make a List of Object and it will still be faster/better than Array.)

avatar image Flame of Endil · Mar 25, 2013 at 03:49 AM 0
Share

Yes, I know, Array is horrible. However, I couldn't use any other collection, because I couldn't figure out how to specify the "type" of a builtin array. How would I go about creating, say, a Generic List (or even a bulitin array) with elements of builtin type? What should I do to translate my code over to a better class?

avatar image Eric5h5 · Mar 25, 2013 at 04:42 AM 0
Share

The type of a builtin String array is String[]. I don't know how representative that code is, but it sounds maybe like you should make a custom class and then make an array or list using that class, rather than using parallel arrays.

avatar image Loius · Mar 25, 2013 at 08:29 AM 0
Share

Right, List, my bad. Too much awake, not enough sleep.

Your String[] arrays could be held in a List.< String[] >

Now we need to find a way to get { } on the same line and we can have all the parentheticals going on.

if ( true ) { var = new List.< String[] >(); }

owch, my eyes

avatar image Eric5h5 · Mar 25, 2013 at 08:39 AM 0
Share

Nice! ;)  

Show more comments

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

14 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

Related Questions

my2dArray.Length(0) 2 Answers

How do I get unity to tell me what type of variable a variable is? 1 Answer

Assets/Gravitation_Simulator.cs(23,47): error CS1061: Type `UnityEngine.GameObject' does not contain a definition for `Length' and no extension method `Length' of type `UnityEngine.GameObject' could be found. 2 Answers

Sorting array 0 Answers

Converting Builtin Array to Generic List 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