I create a 2D array of GO’s in a grid through Instantiate. If I want to create the grid again (with new dimensions), first I destroy the GO’s before making the new grid so they’re out of the scene/hierarchy. Then I create the array again with the “new” command, using the same array name, and fill it with instantiated GO’s. I’m assuming this is all I need to do for garbage collection etc, but just wanted to be sure I’m not missing a command like setting it to null or something.
You don’t have to manually set each index to null, or clear the list. Creating new data in its place will release it to the garbage collector all the same, just make sure you destroy your GameObjects like you’re doing and everything will be peachy.
Thanks!
If I understand correctly:
–I create the GO [,] array and give it dimensions (say 4X5). There are now 20 indices waiting to be populated with GO’s. Those indices are small pieces of memory which will point to the GO’s.
–I set each index to a newly-instantiated GO. The GO is elsewhere in memory (and can be quite large), the index simply points to it. Now that they’re instantiated, Unity knows they are in the scene, so they show up in the Hierarchy.
–I decide to make another array in its place. I destroy the GO’s, so their memory is freed and available for garbage collection. They disappear from the scene/Hierarchy.
–Using the same variable name and the new() keyword, I create a new Array [,] (say 6X10). There are now 60 indices waiting for GO’s. The original 20 indices were not overwritten (probably), they are elsewhere in memory, but are now freed up for garbage collection. (Also I realize there are some differences with how memory is managed if I were to use List instead, as these are dynamic, so I wouldn’t have to use the new() each time.)
–Instantiate the GO’s again, etc.
Correct? Mostly? ![]()