- Home /
The question is answered, right answer was accepted
How to increase the size of a class array?
I've tried using this method to increase the size of my class array by 1 unit:
ClassArray = new ClassVars[ClassArray+1];
But this method clearly removes all the content of the class array and re-creates with the given size which is "ClassArray+1" in this case.
So how do I increase the size of my class array without removing the existing content of the current array?
Thanks in advance, SoumiDelRio
Answer by scipiothegreat · Aug 25, 2013 at 12:00 AM
You could use a generic list.
I'll be adding/removing content from the array, so I think I'm going to learn how to use generic list. I'll test that when I get home.
This forced me to learn something valuable. Thanks for the simple answer :)
Answer by Eric5h5 · Aug 25, 2013 at 12:10 AM
You can use System.Array.Resize, but if you're going to be adding/removing from the array, you'd be better off with a generic List in most cases.
Answer by DaveA · Aug 24, 2013 at 10:59 PM
No, that's about the JS Array class (which should never be used), not built-in arrays, which the question is asking about.
I already tried that before posting my question, does not work.
Answer by trs9556 · Aug 24, 2013 at 11:24 PM
If there isn't a specific method to do that you could make a new array that is the same size as your current array, then store all of your values in that new temp array, increase your array by however much you want (not having to worry about the values because they are being held in another array) then assign the values to that new sized array.
And the page I linked to has code to do what you say. It's called the Scripting Reference and it's essential to read it.
It's also essential to know not to use the Array class. Just pretend it doesn't exist (same for ArrayList). http://wiki.unity3d.com/index.php?title=Which_$$anonymous$$ind_Of_Array_Or_Collection_Should_I_Use%3F