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
1
Question by dansav · Mar 06, 2012 at 01:43 AM · arraylistgenericinitialize

How do you initialize a List or Array?

I'd like to know how to initialize a generic list.

 I have mylist=new List.<String>();
 
 I can't put mylist[14]="hello"; //because it doesn't exist yet.  
 mylist.Insert(14,"hello"); //same problem?  

Do I have to initialize the list somehow?
I tried using things I've seen in a search like ([1..40]) but unity doesn't recognize them.

I can't figure out how to initialize the list with placeholders.

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

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by rutter · Mar 06, 2012 at 03:03 AM

Looks like your list is created, but empty.

 myList.Add("hello");

http://msdn.microsoft.com/en-us/library/3wcytfd1.aspx

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
1

Answer by Eric5h5 · Mar 06, 2012 at 02:31 AM

You did initialize it; that's what "`mylist=new List.();`" does. To add content, use the Add function. http://msdn.microsoft.com/en-us/library/s6hkc2c4(v=vs.80).aspx

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 dansav · Mar 06, 2012 at 02:40 AM 0
Share

$$anonymous$$aybe initialize is the wrong term. I'm trying to add a value at a specific position. I don't have values for positions 0,1,2,3 only 14. The other values will come in later.

avatar image Eric5h5 · Mar 06, 2012 at 02:45 AM 1
Share

The positions in a List don't exist until you add something. Sounds like you want to use myArray = new String[15]; ins$$anonymous$$d.

avatar image dansav · Mar 06, 2012 at 06:06 PM 0
Share

I get an error when trying that: Cannot convert 'UnityEngine.String[]' to 'System.Collections.Generic.List.'

I was hoping to be able to add values at certain positions and that other positions would be null and that I could check to see if a position has a value or is null.

avatar image SirGive · Mar 06, 2012 at 06:10 PM 0
Share

string[] myArray = new string[15];

avatar image Jacob-Aldridge · Mar 06, 2012 at 06:12 PM 0
Share

The List"" is a first-in-first-out array. Using the Generics will cause it to start from zero and go from there. You would have to insert 14 other empty items into the list before getting to the 15th, where you could provide the value you are looking for.

If you are looking for creating a known size array and you plan on using the last one as you need to, I think you'd be better off creating a standard array String[15] as Eric5h5 said.

var mylist = new String[15]; myList[15] = "hello";

All of the other items will remain empty strings until you change them.

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

8 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Remove and Add to List By Name aad 1 Answer

foreach a array inside a generic list 1 Answer

Level generation using a "tree" type structure, why when i add a cell i get an error argument out of range? 1 Answer

Converting Builtin Array to Generic List 2 Answers

how to assign mesh.vertices ToList linq extension 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