x


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.

more ▼

asked Jul 09 '10 at 02:49 PM

Jackpoz gravatar image

Jackpoz
1 1 1 3

Dec 26 '11 at 11:54 PM Fattie
(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

I believe generics (System.Collections.Generic) are not fully supported on iPhone.

There are some good suggestions and details in this Q&A

more ▼

answered Jul 09 '10 at 03:03 PM

Molix gravatar image

Molix
4.8k 17 27 66

more like they are not supported at all, if i can't even get the Count of elements...

Jul 09 '10 at 03:20 PM Jackpoz

indeed, had to use an ArrayList....

Jul 09 '10 at 03:48 PM Jackpoz
(comments are locked)
10|3000 characters needed characters left

Don't construct a List with an array, it crashes on the phone. Known bug.

Instead add the objects one at a time.

more ▼

answered Jul 09 '10 at 04:02 PM

Tetrad gravatar image

Tetrad
7.3k 27 37 89

i hackfixed it using the non-generic ArrayList type. ArrayList gos = new ArrayList(GameObject.FindGameObjectsWithTag(tag));

Jul 11 '10 at 09:51 AM Jackpoz
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x2013
x1396
x382
x183
x133

asked: Jul 09 '10 at 02:49 PM

Seen: 2767 times

Last Updated: Dec 26 '11 at 11:54 PM