• Unity
  • Services
  • Made with Unity
  • Learn
  • 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
  • Forums
  • Answers
  • Feedback
  • Issue Tracker
  • Blog
  • Evangelists
  • User Groups

Navigation

  • Home
  • Unity
  • Industries
  • Made with Unity
  • Learn
  • Community
    • Forums
    • Answers
    • Feedback
    • Issue Tracker
    • Blog
    • Evangelists
    • User Groups
  • Get Unity
  • Asset Store

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
10

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

No one has followed this question yet.

Related Questions

A node in a childnode? 1 Answer

List gives "Array index is out of range" for no reason 2 Answers

How do I check for strings in an array to match up with the index of another array? 1 Answer

How to arrange list 3 Answers

How do I compare the position of my player to each elements position of an array of GameObjects? 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges