- Home /
Using List.Count on Unity iPhone
this is a little piece of code that works like a charm in the Unity iPhone Editor but doesn't work on the iPhone, throwing an exception in the XCode console at runtime.
GameObject[] gos = GameObject.FindGameObjectsWithTag("TagUsedIngame");
//gos size is 4 in my test
List<GameObject> gosList = new List<GameObject>(gos);//<-- exception
the exception is documented at http://monotouch.net/Documentation/Troubleshoot
System.ExecutionEngineException: Attempting to JIT compile method (wrapper managed-to-managed) Foo[]:System.Collections.Generic.ICollection`1.get_Count () they suggest to write something like
Foo [] array = null;
int count = ((ICollection<Foo>) array).Count;
which is pretty funny since this code will throw a null deref exception^^
did anyone already solve this issue? It's more related to Mono than to Unity, because it works fine in the Editor. I set .NET in the Player Settings of the Project to 2.1 . Thank you for the time spent reading this wall of text.
FYI Note this $$anonymous$$UCH NEWER answer from 2011
http://answers.unity3d.com/questions/54841/does-current-iphone-support-listt-in-c.html
Answer by Tetrad · Jul 09, 2010 at 04:02 PM
Don't construct a List with an array, it crashes on the phone. Known bug.
Instead add the objects one at a time.
i hackfixed it using the non-generic ArrayList type. ArrayList gos = new ArrayList(GameObject.FindGameObjectsWithTag(tag));