I haven’t been able to find a solution to this searching here or figure this out on my own so I’m wondering how can I delete an object/item in a list? I’m trying to filter out all objects except “turret_Body” in the list.
Edit: I am trying to remove the object from the list, not destroy the object it references.
Here’s what I tried (among other ways):
var friends = GameObject.FindGameObjectsWithTag("Player");
for (var friend : GameObject in friends)
{
if (friend.name != "turret_Body")
{
// delete friend;
}
}
The “delete friend” line doesn’t work when uncommented, it stops the game. Couldn’t find any delete, remove or list operations.
I can find any help for lists in Unity, is it a more general JavaScript thing?
Hey, I tried that but it seems to actually destroy the object. I’m more looking just to pull it out of the list that is returned from FindGameObjectsWithTag(). I need to use the list, but first want to remove certain objects from the list (but not destroy the GameObjects).
friend = null causes an empty item int he array, which i could work with but I’d rather remove it (or I’ll have to always check for empty objects elsewhere in the code)
Thanks, that helps me a lot. I looked and it seems that in order to remove a specific item in the array I have to use friends.RemoveAt(index), my only problem now is how to know what array index the current “friend” is (in my for statement).
Do I have to manually count it as I loop through (And then reducing the count as I remove elements)? Is that the only way to find out the current friend’s array index?
The Array class doesn’t have a search function, so you have to iterate through the array using a “for” loop. You don’t have to decrement the count as long as you use array.length in the loop condition:-
for (i = 0; i < nums.length; i++) {
if (thisOne) {
nums.RemoveAt(i);
}
}
Thanks everyone so far… I’m close but still haven’t worked this out. Andee, I’m doing like you say but for some reason I error on the RemoveAt(). I’ve simplified it down to this:
var friends = GameObject.FindGameObjectsWithTag("Player");
for (i = 0; i < friends.length; i++) {
if (friends[i].name != "turret_Body")
friends.RemoveAt(i); // ** errors on this line
}
But for some reason it errors with:
BCE0019: ‘RemoveAt’ is not a member of ‘(UnityEngine.GameObject)’.
This just seems odd to me because friends is an array not a GameObject, like the error says.[/b]
Any more suggestions?
Edit: When doing a Debug.Log(friends) the result is UnityEngine.GameObject[ ].
Edit2: I thought those [ ]'s meant that it is an array, but I seem to be wrong about that.
“friends” isn’t an Array, it’s (UnityEngine.GameObject), like the error says. FindGameObjectsWithTag returns GameObject[ ], which the error writes as (UnityEngine.GameObject), which is probably not ideal (more than one person has failed to notice that). A single GameObject would just be UnityEngine.GameObject, without the parentheses. You have to convert; RemoveAt only works with dynamic Javascript arrays and nothing else.
Okay, I will look into this some more. According to the manual FindGameObjectsWithTag “Returns a list of active GameObjects tagged tag”.
So the only way I can filter or remove them out of this list is to convert this list to an array? And this is because this list is not a dynamic Javascript array?
Thank you again; I will try this…
Edit: I’m re-reading the Array in the docs (as you suggested earlier) and it’s making more sense now since your last post. I think this list is a Builtin array, right?
Actually the docs also specify exactly what is returned:
static function FindGameObjectsWithTag (tag : string**) : GameObject[ ]**
The bit after the colon at the end says what is returned by the function. The page on Arrays explains about the differences between Javascript Arrays and builtin arrays, and how to convert between them.
Edit: Just saw your edit, I think we are cross-posting.
Ah! Now I notice the static is what how I know it’s a Builtin array. Thanks. I’m pretty much learning JavaScript through Unity3D for the first time. But tonight I learned a lot more about arrays. I’m coming from Blitz3D so JavaScript has a lot more to it, but I like it a lot!
I was able to get it working now with just one new line. For anyone who wants the solution:
var friendsJs = new Array (GameObject.FindGameObjectsWithTag("Player"));
for (i = 0; i < friendsJs.length; i++) {
if (friendsJs[i].name == "turret_Foot") // remove foot
friendsJs.RemoveAt(i);
}
Actually no, sorry, GameObject[ ] is how you know it’s a builtin array. “Static” is something else (basically it’s about the scope of the function, but it’s not really something you have to worry about).