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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by TheMaster42 · Apr 14, 2013 at 09:02 AM · javascriptarraysjagged

Syntax to initialize 2D (Jagged), built-in array at runtime

I want to create a "jagged" built-in array of GameObjects in Unity Script. The first dimension will always have 4 elements, but I want to define the length of each sub-array at runtime. I've tried:

 private var armorBarReferences : GameObject[,];
 armorBarReferences = new GameObject[4];

Which returns: Cannot convert 'UnityEngine.GameObject[]' to 'UnityEngine.GameObject[,]'.

I also tried:

 private var armorBarReferences : System.Array[];
 armorBarReferences = new System.Array[4];

But with the second case, something like:

 armorBarReferences[currentArc][i] = Instantiate(armorBarObject, position, rotation);
 armorBarReferences[currentArc][i].transform.localScale += Vector3(1, 1, 1.1);
 

Gives: 'transform' is not a member of 'Object'.

I think I could do this with a JavaScript Array class as the first dimension, but wanted to learn the "right" way. The size of the arrays will never change once I've set them, so the built-in arrays seemed the right choice.

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

1 Reply

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

Answer by Azzara. · Apr 14, 2013 at 10:04 AM

GameObject[,] is a not a jagged array. It is called a multi-dimensional array. GameObject[][] is a jagged array. Which one do you want to use? If all 4 elements have the same length n, then yes, your multi-dimensional array GameObject[,] is the right choice.

However, you can't change the size of the second dimension of a GameObject[,] at runtime. You have to set both sizes when you initialize the array. Like this

   var n = 100; // decide what element length to use at runtime
   armorBarReferences = new GameObject[4, n];

Edit: If you need a jagged array GameObject[][], it is a little tricky to declare it in Javascript. You can try this

 private var dummy : GameObject[] = null;
 private var armorBarReference = [ dummy, dummy, dummy, dummy ];
Comment
Add comment · Show 6 · 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 TheMaster42 · Apr 14, 2013 at 05:46 PM 0
Share

Ah, I want to use a Jagged array, since all four elements will have different lengths. This looks right, but private var armorBarReferences : GameObject[][]; gives me the error: ';' expected. Insert a semicolon at the end.

Is Unity not able to define built-in arrays in this manner still?

avatar image Eric5h5 · Apr 14, 2013 at 06:30 PM 0
Share

Not with Unityscript, only C#. With Unityscript you can only declare them implicitly, such as var foo = [[1, 2], [3, 4, 5], [6]];

avatar image TheMaster42 · Apr 14, 2013 at 07:20 PM 0
Share

So is there no way to initialize my jagged array after the declaration?

avatar image Eric5h5 · Apr 14, 2013 at 07:25 PM 1
Share

It would probably be easier just to use a List of GameObject arrays. There are no issues doing that in Unityscript and it has the same effect as a jagged array.

avatar image Azzara. · Apr 14, 2013 at 08:11 PM 1
Share

In C# you could write

 private GameObject[][] armorBarReference = new GameObject[4][];

I've managed to achieve the same in Javascript.

 private var dummy : GameObject[] = null;
 private var armorBarReference = [ dummy, dummy, dummy, dummy ];

In both cases you get a new GameObject[4][]. The first dimension is set to 4. The second dimension is not set yet. To set the dimension of the fourth element to 100 try

 armorBarReference[3] = new GameObject[100];
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

My array does not update when object is destroyed. How do I fix it? (java) 2 Answers

For values in array 2 Answers

JesseEtzler's RPG Talent System in C# 1 Answer

Send a message to multiple scripts within an Array - Javascript 1 Answer

How to create an auido array (JavaScript) 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