1
public void FindClosestEnemy()
{
    List<GameObject> pList = GameObject.FindGameObjectsWithTag("Player");                             
    pList.OrderBy(obj=>Vector3.Distance(FindLocalPlayer().transform.position, 
obj.transform.position)).ToList();
}

I do not understand the difference between the two lists. How do I convert the 'UnityEngine.GameObject[]' list to the System.Collections.Generic.List>UnityEngine.GameObject<

2 Answers2

3

GameObject.FindGameObjectsWithTag returns an array of GameObjects.

An array in C# is a data structure that stores multiple objects of the same type.

A list is a generic collection of objects.

While an array and a list are very similar in concept, there are different between the two in terms of data access and usage. Generally an array is only populated once at creation and only read thereafter, while a List can have it's elements altered at any time (some caveats apply).

This StackOverflow question summaries why you might want one over the other: Array versus List<T>: When to use which?

In your specific case, you want to apply some Linq ordering to your GameObject collection so you need to convert the array received from FindGameObjectsWithTag to a list.

You can do this in several ways. The easiest is to use a constructor overload on list to assign the entire array at once:

GameObject[] gameObjectArray = GameObject.FindGameObjectsWithTag("Player");
List<GameObject> gameObjectList = new List<GameObject(gameObjectArray);

Some other options are available here: Conversion of System.Array to List

Steve
  • 8,822
  • 10
  • 42
  • 73
  • Why is List better with Linq? Linq works with arrays too. – captainCrazy Apr 13 '18 at 07:07
  • @captainCrazy fair point, I tend to use Lists almost exclusively. I rarely come across a situation where an array is a better solution. Perhaps memory wise they're better? – Steve Apr 13 '18 at 07:45
  • I also almost always use Lists. I only used Linq with arrays when I wanted to sort the output of a Physics.OverlapSphere right there and then. If I had to guess, I think arrays can be better performance-wise as the elements are all sequential. Memory access and stuff. – captainCrazy Apr 13 '18 at 07:52
1

As Steve rightly explain but i want to answer in a bit different manner:

Actually in your code you are assigning array type object (GameObject.FindGameObjectsWithTag("Player")) into list type (List<GameObject> pList ) object which is not implicitly support by the compiler. You have to convert your list into an array type:

List<GameObject> pList = new List<GameObject>(GameObject.FindGameObjectsWithTag("Player"));

After this, you will be able to use your code.

Now for the first part of the question

I do not understand the difference between the two lists.

Actually you are asking about Different b/w array and list and the main difference you should need to keep in mind that when you have fixed length of data then, you should use array and if you have variable lenght of data then you use List.

You can also find great detail on this topic on stackoverflow

  1. Array versus List: When to use which?
  2. How and when to abandon the use of arrays in C#?
  3. Why do we use arrays instead of other data structures?
  4. List explaniation by MSDN
  5. Arrays considered somewhat harmful
  6. Should I user array or list
Muhammad Faizan Khan
  • 8,187
  • 11
  • 74
  • 149