Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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
4
Question by ChrisSch · Mar 09, 2014 at 01:55 AM · listresizelengthgeneric list

Set size of generic list via script?

I just started using lists and its a bit confusing to me. How can I set the size of my list? Count is read only and Capacity doesn't do it either. With arrays you'd do something like:

 array1.length = array2.length;

But how do you do it with generic lists? I tried:

 list1.Capacity = list2.Count;

or

 list1 = new List<int>(list2.Count);

and some other variations, but it doesn't resize it in the inspector; I can do it manually but I wanna do it via script if possible.

I've read through the http://msdn.microsoft.com/en-us/library/6sh2ey19(v=vs.110).aspx but couldn't find anything that would let me do that.

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 superluigi · Mar 09, 2014 at 02:17 AM 1
Share

I just started using them too. From my very limited knowledge, you can't set a permanent size. link text

avatar image ChrisSch · Mar 09, 2014 at 02:24 AM 0
Share

Yes I went through that too and nothing. Lets wait and see if anyone else can tell us if we can. xD $$anonymous$$aybe we can make an empty array of some length and convert it into a list. I'm not sure, I can't find anything on that either.

I'm liking lists tho, not faster than builtin arrays, but faster than javascript arrays, and have type casting, and more functions that builtin arrays don't.

2 Replies

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

Answer by Eric5h5 · Mar 09, 2014 at 02:23 AM

You don't explicitly set the size of a List; the size comes from the number of items in it. (Capacity is only for the internal size of the List and something you would rarely if ever use.) If you want a List with a particular number of items you can convert an array to a List.

 list1 = new List<int>(new int[list2.Count]);
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 ChrisSch · Mar 09, 2014 at 02:30 AM 0
Share

Aaand there's the answer I was expecting! xD I'm following a tutorial which uses javascript arrays, and doesn't use #pragma strict which makes it not work on mobile platforms, me thinks. So I decided to use lists ins$$anonymous$$d and I'm trying to follow it as closely as possible because I know I'm bound to run into speed bumps at every step. xD

Thank you!

avatar image Eric5h5 · Mar 09, 2014 at 02:33 AM 0
Share

Bleh, nothing should ever use Javascript arrays. ;) Especially not a tutorial, since that's likely to confuse people who aren't aware of the problems with them.

avatar image ChrisSch · Mar 09, 2014 at 02:48 AM 0
Share

It's an about 2 years old tutorial by Eteeski on AI pathfinding. I've been struggling for days and sleepless nights to do some simple A* or node based or raycast or whatever kind of AI pathfinding to no avail. I understand it all and how A* works but I don't know how to write it into code and then I get confused and lost, and end up rocking in my chair back and forth, laughing, and crying. Ok that last part didn't actually happen. xD

I'm aware of Aron Granberg's pathfinding solution and other similar ones, but I really wanna learn to do it, not use someone elses code without understanding and knowing how to do the basics of it. But the lack of tutorials on it, and 97% of them being in C# is killing me. xD

avatar image Eric5h5 · Mar 09, 2014 at 02:59 AM 0
Share

Well, at least it's straightforward enough in most cases to convert Javascript arrays to generic Lists...if you get stuck on anything just post another question.

avatar image ChrisSch · Mar 09, 2014 at 03:02 AM 0
Share

Ok, thanks again! :D

Show more comments
avatar image
2

Answer by JoeStrout · May 03, 2017 at 03:56 PM

This page comes up pretty early in searches for how to resize a list. So, here's my solution — just stick this in a (possibly static) class somewhere, and it adds a Resize(n) method to the generic List.

 public static void Resize<T>(this List<T> list, int newCount) {
     if (newCount <= 0) {
         list.Clear();
     } else {
         while (list.Count > newCount) list.RemoveAt(list.Count-1);
         while (list.Count < newCount) list.Add(default(T));
     }
 }
Comment
Add comment · Show 2 · 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 michael_house · May 03, 2017 at 04:58 PM 0
Share

Cleaner solution is to use Linq: http://stackoverflow.com/questions/31656460/c-sharp-enumerable-take-with-default-value

avatar image JoeStrout michael_house · May 03, 2017 at 05:04 PM 0
Share

Are you referring to .Take? That can only shorten the list, and it does so by making a new list, which is wasteful. The Resize extension method above can both shorten and extend, and does so very efficiently (no copies).

(That SO page includes some extension methods to address this, some of which are functionally the same as my solution above.)

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

24 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

A node in a childnode? 1 Answer

Can you set a string length when declaring it to a certain other number? 2 Answers

Get the two highest floats in a genericList without using .Sort()? 1 Answer

Count selected item in List 1 Answer

Get size of a vector3 array c# 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