Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jul 24th, 2012  |  syntax: None  |  size: 2.17 KB  |  hits: 13  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Convert Bound List to Generic List<T>
  2. List<Component> compList = new List<Component>();
  3.         foreach(GameObject obj in objects)   // objects is List<GameObject>
  4.         {
  5.             var retrievedComp = obj.GetComponent(typeof(T));
  6.             if(retrievedComp != null)
  7.                 compList.Add(retrievedComp);
  8.         }
  9.  
  10.         List<T> newList = new List<T>(compList as IEnumerable<T>); // ERROR HERE
  11.  
  12.         foreach(T n in newList)
  13.             Debug.Log(n);
  14.        
  15. ArgumentNullException: Argument cannot be null.
  16. Parameter name: collection
  17. System.Collections.Generic.List`1[TestPopulateClass].CheckCollection (IEnumerable`1 collection)
  18. System.Collections.Generic.List`1[TestPopulateClass]..ctor (IEnumerable`1 collection)
  19. DoPopulate.AddObjectsToList[TestPopulate] (System.Reflection.FieldInfo target) (at Assets/Editor/ListPopulate/DoPopulate.cs:201)
  20. System.Reflection.MonoMethod.Invoke (System.Object obj, BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture)
  21. Rethrow as TargetInvocationException: Exception has been thrown by the target of an invocation.
  22. System.Reflection.MonoMethod.Invoke (System.Object obj, BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture)
  23. System.Reflection.MethodBase.Invoke (System.Object obj, System.Object[] parameters)
  24. DoPopulate.OnGUI () (at Assets/Editor/ListPopulate/DoPopulate.cs:150)
  25. System.Reflection.MonoMethod.Invoke (System.Object obj, BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture)
  26.        
  27. List<T> newList = compList.OfType<T>().Select(x=>x).ToList()
  28.        
  29. List<T> compList = new List<T>(); // +++ T instead of GameObject
  30.     foreach(GameObject obj in objects)   // objects is List<GameObject>
  31.     {
  32.         // ++++ explict cast to T if GetComponent does not return T
  33.         var retrievedComp = (T)obj.GetComponent(typeof(T));
  34.         if(retrievedComp != null)
  35.             compList.Add(retrievedComp);
  36.     }
  37.  
  38.     List<T> newList = new List<T>(compList as IEnumerable<T>); // ERROR HERE
  39.  
  40.     foreach(T n in newList)
  41.         Debug.Log(n);