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
2
Question by LifeSizeDeity · Jun 24, 2010 at 05:50 PM · arraynew

Is there a way to declare an array without using a size?

This is just a question of the interaction between Unity3d and a c# script. Currently I am using a script that makes the call:

public int [] foo = new int[10];

The thing that's getting me is inside the inspector window, I am able to change the size of the array. Obviously its size must be dynamic, so I guess my question is: Is there a way to declare the array without assigning its size, since I can just change it in the inspector window anyways? Also, how would I get the size of the array if it were to be changed in the inspector?

Comment
Add comment · Show 1
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 yoyo · Oct 13, 2010 at 11:20 PM 0
Share

Note that the size isn't actually dynamic, rather, when you "resize" the array in the editor it will actually create a new array (of the new length) and copy your old data into it.

2 Replies

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

Answer by Peter G · Jun 24, 2010 at 06:03 PM

There are several options.

1.. It sounds like what you want to do is just this. It creates an array that can be resized/ edited in the inspector.

 public int[] foo;

2.. Or if you are trying to resize your array frequently in-game, you might want to consider using ArrayLists or GenericLists. Generic lists will show up in the inspector, but arraylists will not.

To access the size of an array use array.Length. To access the number of items in a generic list use list.Count.

ex.

if(foo.Length == 5) {
     print("foo has 5 elements");
}
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 LifeSizeDeity · Jun 24, 2010 at 08:28 PM 0
Share

Thanks so much, that takes care of all my questions.

avatar image
1
Best Answer

Answer by Molix · Jun 24, 2010 at 06:12 PM

To answer your second question you can get the size using the Length property, e.g.

int sizeOfArray = foo.Length;

Also, you can resize the built in arrays using static members of the Array class, e.g.

Array.Resize<int>( ref foo, foo.Length + 1 );  // increases array size by one

Though as mentioned, it is often easier to use List<> if you'll do that often. The nice thing about normal arrays is they're faster 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

No one has followed this question yet.

Related Questions

Picking a Random Order for Levers 1 Answer

Adding variables from all scripts 2 Answers

A problem with arrays 1 Answer

Different footstep sounds problem! 2 Answers

Instantiate duplicates script in subsequent Objects 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