Search code examples
javascriptarraysnew-operatorunityscriptdelete-operator

How to delete new'd arrays in JavaScript?


I'm using Unity, so this is technically UnityScript but I assume the built-in arrays work the same way. I have an array I create thusly:

var room:roomInfo[,,];
room=new roomInfo[5,5,5];

What's the deleting syntax? Simply doing delete room; or delete room[,,]; doesn't seem to work. After deleting it I need to do this:

room=new roomInfo[10,10,10];

Seems like a ridiculously simple question but I get confused about these things and forget, and I couldn't find anything on Google (because I couldn't work out what to search).


Solution

  • You don't need to do it, as UnityScript already does it for you (Automatic Memory Management). But if you still prefer your own way to do, use this:

    room = null;
    

    And after this, you can initialise it to:

    room = new roomInfo[10,10,10];
    

    So the full code would be:

    room = null;
    room = new roomInfo[10,10,10];