Answer the question
In order to leave comments, you need to log in
How to create an array of objects without size (without List and Dictionary)?
For certain reasons, I need to create arrays of objects GameObject[] ArrayObj
But I don’t always know how many there will be, as I understand it is impossible to set an array of indefinite size, also for certain reasons I don’t want to use List or Dictionary (we won’t go into details why) ...
So far I have 2 ideas how to do it:
1. Create an array with much more elements
, for example, set 10 objects there and when pulling them out like this
GameObject[] ArrayObj = new GameObject[100];
foreach(GameObject obj in ArrayObj){ if(obj != null){ ........... }}
GameObject[] ArrayObjTemp = new GameObject[100];
foreach(GameObject obj in ArrayObjTemp){ if(obj != null){ ++n }}
GameObject[] ArrayObj = new GameObject[n];
foreach(GameObject obj in ArrayObjTemp){ if(obj != null){ ArrayObj[n] = obj; ++n }}
Answer the question
In order to leave comments, you need to log in
Reading the comments - the reasons why you want to use arrays instead of List`1 are
speculation, and based on nothing.
In practice, you will not feel any difference between a List and an array in terms of memory and speed.
So take System.Collections.Generic.List<T>
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question