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 MJG1123 · Nov 05, 2015 at 09:10 PM · listsizelimitcards

Limit the size/capacity of a list

I tried setting the listname.capacity in both start and update and even tried listname.capacity.set but it doesn't exist

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 gjf · Nov 05, 2015 at 08:24 PM 0
Share

what are you trying to do? capacity just sets/reports the limit when the list requires resizing, not limiting the size of it iirc, you'll need to handle that yourself...

avatar image SeasiaInfotechind · Nov 06, 2015 at 05:05 AM 1
Share

If you want limited size, use Array.

3 Replies

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

Answer by wibble82 · Nov 06, 2015 at 12:31 PM

Hi

The list 'capacity' does not place a fixed size on the list - it is there to tell you how many items the list could currently store without allocating more memory (as it allocates memory out in blocks). When you set it, you are telling the list 'I expect there to be this many items' - it's a hint to avoid lots of little memory allocations.

Is no in built function to add to a list without going over a certain size - indeed, the whole point of 'List' is that it is a dynamically sizing array!

If you want fixed size behaviour you'll either want to:

  • just use an array (though this doesn't behave quite like a list)

  • write your own function that only adds to the list if it is not already full

-Chris

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

Answer by $$anonymous$$ · Aug 23, 2018 at 05:19 PM

hi there

try this

     public Unit unit;

     public int armyCapacityLimit = 10;

     public void RecruitUnits()
     {
         if (armyCapacity.Count < 10)
             armyCapacity.Add(unit);
         else
             return;
     }
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
avatar image
0

Answer by HenryStrattonFW · Nov 06, 2015 at 11:18 AM

For a fixed size, consider using an array, If for whatever reason you do really want a list. then you need to encapsulate access to the list so that you can force a size restriction when you try to add to the list.

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 Solihin100 · May 17, 2021 at 10:00 AM 0
Share

Could you please elaborate more? What is and how to use 'encapsulate access'?

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

8 People are following this question.

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

How to extend tree size to more than 200? 0 Answers

how to access list from another class (list of cards on deck class) ? 1 Answer

List limits element size 2 Answers

List of type Button has size set to zero at runtime 0 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