- Home /
Should I manually delete my custom class instances?
I'm only just getting into custom classes... So I'm creating a class called Chunk:
class Chunk { public static var cornerPos = new Vector3[3]; function Chunk(){} // Constructor }
// Create an array of chunks var chunks = new Chunk[10];
If I remake the chunks array - for instance to resize it, add or delete chunks - will the actual instances of the Chunk class still remain? As I understand it, if I use the Instantiate command with a variable...
var clone = Instantiate(chunkClone, Vector3(0,0,0), Quaternion.identity);
... if I change that variable (or it's declared in a function and so disappears at the end of the function), the instantiated GameObject still remains in the hierachy as the variable was just a reference to it. Is it the same thing with my custom class?
For example, I might use the following to resize the array:
var chunksTemp = chunks;
chunks = new Chunk[9];
for (a=0;a<9;a++) {
chunks[a] = chunksTemp[a];
}
Will there now be 19 instances of the Chunk class in memory, 10 of them just floating in the ether, or do those 10 instances get somehow cleared away when I redeclare the array? And if the initial 10 instances created do stay behind, how do I destroy them manually?
Answer by Mike 3 · Aug 05, 2010 at 05:23 PM
If you resize the array and nothing else is referencing the variables, the garbage collector will clean up the instances for you, so no need to do it yourself. On the other hand, you actually can't do it yourself, so it's a moot point anyway