Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 allenrobot · May 03, 2017 at 11:41 AM · javascriptarrayfunction

Why does this passed Array cause errors in a function?

 function Afunction(x:String,y:int){//THIS ALL WORKS WITH TheArray being directly called
       var Myoutput = new String[TheArray[y].length];
       Myoutput[0] = x + " " + TheArray[y][0];
       for(var i : int = 1; i<(TheArray[y].length);i++){
         Myoutput[i] = SomeOtherArray[0][ConvertToSomething(x,TheArray[y][i])];
       }
       return Myoutput;
 }
 
 function AMoreDynamicfunction(x:String,y:int,ThePassedArray:Array){//This throws errors with ThePassedArray being seen as some object
       var Myoutput = new String[ThePassedArray[y].length];
       Myoutput[0] = x + " " + ThePassedArray[y][0];
       for(var i : int = 1; i<(ThePassedArray[y].length);i++){
         Myoutput[i] = SomeOtherArray[0][ConvertToSomething(x,ThePassedArray[y][i])];
       }
       return Myoutput;
 }


 private var TheArray = [ 
 ["word","1","2","3","17","2"],
 ["words and stuff","1","2","3"],
 ["another word","1","2","3","4"]];

The compiler error happens before I even get to set up a call to the second (more dynamic) version.

The first function runs perfectly but I wanted to create a similar function using only a different array so I thought it would be great to only need one function and make the array a passed variable so I can just send any array to the one new function. This however gives these errors:

'length' is not a member of 'Object'. Type 'Object' does not support slicing.

Is there any way to do this in UnityScript (js) or am I going to have to make the same function again with a different array directly called inside like the first one? I could probably copy the array to another array right before the function call and then directly call that changing array in one function.. but isn't that the whole point of passing an array to a function? Why is this not working? P.S. in case people wonder TheArray[y].length is correct, It is not meant to be TheArray.length. I am specifically doing this to check the length of an array within an array which works fine in the first function. The first function is 100% working.

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 bobisgod234 · May 04, 2017 at 02:24 AM 0
Share

Can you show the code where you define "TheArray" and where you are calling "A$$anonymous$$oreDynamicfunction"?

avatar image allenrobot bobisgod234 · May 04, 2017 at 02:38 AM 0
Share

I added, however..note that first function works 100% and the second array is just like the first one. (An array of arrays of varied length with only string values) Except I have it as a different array because different parts of the program use it separately in a different way than the first array. (Also cleaner and easier that way for stuff down the line). These are not 2 functions running at the same time or in the same program. I was wanting to kill the first one and use the second with different arrays I pass to it.

2 Replies

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

Answer by bobisgod234 · May 04, 2017 at 04:25 AM

The Array seems to return instances of type Object, so you need to typecast the object to type Array to use it as an array.

 function AMoreDynamicfunction(x:String,y:int,ThePassedArray:Array){//This throws errors with ThePassedArray being seen as some object
     var innerArray = ThePassedArray[y] as Array; // type cast object to array
     var Myoutput = new String[innerArray.length];
     Myoutput[0] = x + " " + innerArray[0];
     for(var i : int = 1; i<(innerArray.length);i++){
       Myoutput[i] = SomeOtherArray[0][ConvertToSomething(x,innerArray[i])];
     }
     return Myoutput;
 }
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 allenrobot · May 04, 2017 at 04:36 AM 0
Share

Very good! Thank you. I wonder why this is an issue though. I figured it was getting passed into it through its parent array so it would remain intact. Interesting! This should help others with the same unityscript oddity with passing arrays of arrays. If anything, I need to remember that if this sort of issue happens anywhere else, I should force type cast on thing in question. Thanks again!

avatar image
0

Answer by zacb_dz_123 · May 04, 2017 at 01:27 PM

I think the problem is when you pass the Type " :Array" in the function parameters of

function AMoreDynamicfunction(x:String,y:int,ThePassedArray:Array)

it doesn't recognize the Type you are trying to access.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Storing functions in a list/array? 1 Answer

passing a javascript array into a function sorts it? 1 Answer

Passing an Array 1 Answer

How to call a function with Vector3[] argument? 3 Answers

Loop Through Entire Array - Simon Game 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