- Home /
How do I delete or empty an array of custom structs during edit mode?
I have an array of structs, KeyFrame containing an int and a Transform, to which I can add elemnts to it via a custom GUI button I made.But I don't know how to delete elements. I thought there would be a Delete, Empty, Clear or similar function when handling arrays in C# compared to C++, but I no such thing seems to exist.
How do I delete an entire array or remove a single element?
Answer by Larry-Dietz · Dec 01, 2019 at 02:24 PM
Consider using a List instead of an array, With the List, you can easily remove a single entry with .RemoveAt.
As far as clearing an array, you should be able to reinitialize it at a 0 length like this...
arrayvariable = new structname[0];
The only way I know to remove a single element from an array is to create a new array and then copy everything from the old except the one you want to remove.
I am sure there is a better way, but I have never looked for it, as I just use a List when I want to be able to modify it in code.
Hope this helps, -Larry