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
5
Question by ronronmx · Jan 19, 2011 at 09:30 PM · arraylist

Difference between List<> and Array[]

So I was becoming familiar and comfortable with Arrays, but I recently started using Lists and I'm a little confused about the following code:

private GameObject[] sceneObjects; sceneObjects = GameObject.FindGameObjectsWithTag ("Clone");

List<GameObject> sceneObjects= new List<GameObject>(); sceneObjects = GameObject.FindGameObjectsWithTag ("Clone");

The first method of adding GameObjects tagged "Clone" to the sceneObjects Array works fine, but it doesn't work with the sceneObjects List.

I thought Lists were very similar to Arrays, so why can't I store my tagged objects the same way?

Thanks!

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
7
Best Answer

Answer by Jesse Anders · Jan 19, 2011 at 09:36 PM

Lists and built-in arrays are different types, and direct assignment from one to the other isn't supported. However, one of the constructors for List accepts an iterable container as its argument. I'd have to check to be absolutely certain this will work, but I think you should be able to do the following:

var sceneObjects =
    new List<GameObject>(GameObject.FindGameObjectsWithTag("Clone"));

That should have the same end effect that your second code example is intended to have.

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 Tetrad · Jan 19, 2011 at 09:56 PM 4
Share

alternatively, you can do sceneObjects.AddRange( GameObject.Find.... );

avatar image ronronmx · Jan 19, 2011 at 10:20 PM 0
Share

Thank you guys, that did the trick ;)

avatar image
14

Answer by yoyo · Jan 20, 2011 at 07:30 AM

The key difference between a list and an array is that an array is fixed size while a list is dynamic -- you can add new elements to a list, but to add a new element to an array you need to create a new larger array and copy the old elements.

Arrays are a little leaner and more efficient if you don't need dynamic resizing, but lists are usually more convenient and the performance difference rarely matters for most applications.

Much more info and opinions here ... http://stackoverflow.com/questions/434761/array-versus-listt-when-to-use-which

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

1 Person is following this question.

avatar image

Related Questions

A node in a childnode? 1 Answer

Creating a record of values generated with Random.Range 1 Answer

Need help with switch case in order/timed 0 Answers

Create a button from an int, remove button when clicked, and never load it again 1 Answer

Can't add GameObjects to ArrayList 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