How to get a List<> from GetComponents?

GetComponents returns a []. Is there any one-liner way to convert that to a Generic (List or whatever)?





Answer by ArkaneX 

With LINQ (requires using System.Linq;):

  1. var list = GetComponents<SomeComponent>().ToList();

Without LINQ:

  1. var list = new List<SomeComponent>(GetComponents<SomeComponent>());

Code in C#.





ref : https://answers.unity.com/questions/630680/how-to-get-a-list-from-getcomponents.html

+ Recent posts