why this does not completly remove the array elemets:
//ERASE THE OLD ARRAY
for(i=0;i<groupList.length;i++){
Debug.Log("index " + i.ToString() + "erase: " + groupList[i].name);
groupList.RemoveAt(i);
}
why this does not completly remove the array elemets:
//ERASE THE OLD ARRAY
for(i=0;i<groupList.length;i++){
Debug.Log("index " + i.ToString() + "erase: " + groupList[i].name);
groupList.RemoveAt(i);
}
i tried this also, it does not work:
for(i=0;i<groupList.length;i++){
groupList[i]=null;
}
//ERASE THE OLD ARRAY
for(i=0;i<groupList.length;i++){
//Debug.Log("index " + i.ToString() + "erase: " + groupList[i].name);
if(groupList[i]==null){
groupList.RemoveAt(i);
}
}
basically i want to delete elements of array and recreate it…
groupList = new Array(groupList.length);
You have my permission to slap yourself in the face now.
Problem with Cameron’s method is that if other scripts already have a reference to that array, they’ll still retain all the original data.
The problem with the loops you have there is they go through from 0 to Length, but as you remove entries, the array rebuilds itself so you end up skipping entries. Instead, loop through backwards:
for (var i : int = groupList.Length; i >= 0; i--)
{
groupList.RemoveAt(i);
}
EDIT: I didn’t know that RemoveAt existed for an array. What class are you using?
If you’re using System.Collections.Generic.List (which you might want to consider switching to if you aren’t) then it has a Clear() method on it which does exactly what it sounds like.
i dont use System.Collections.Generic.List
i would like to learn to use it thought but there arent that much examples out there. do you have some good resource that you want to share,
i would appreciate!
if there is any for javascript
//ERASE THE OLD ARRAY
groupList.Clear();
… or if you have your heart set on using RemoveAt …
//ERASE THE OLD ARRAY
while (groupList.length > 0)
groupList.RemoveAt(groupList.length - 1);
Actually a lot of this depends on what you have in the Array.
That’s the problem with Javascript, no way of knowing from looking at your code what that is an array of.
If they’re GameObjects/Components/Assets you’ll need to call Destroy() on each object in the list before you clear or remove them if you actually want them to dissapear from the game world.
http://msdn.microsoft.com/en-us/library/6sh2ey19.aspx
The only difference with Javascript (aside from the usual C#/JS differences) is the syntax uses an extra “.” for some reason, so “List” would be “List.”.
That’s not a problem with Javascript; it wouldn’t be any different in C#.
–Eric