- Home /
Removing element from array by value, not index
Hi,
I got an array. One of the elements inside this Array is "item_Key". I would like to know how I can remove this item from the array. I am basically hitting a box, which will trigger to remove "item_Key" from the array. The only problem is I don't know how to do this. I know I can remove an index with RemoveAt() but I am looking to remove by the value.
gameObject.Find("itemHolder").GetComponent("playerInventory").playerInventory.RemoveAt();
@Andreeeee
var RemoveItem : String;
RemoveItem = "item_Key";
var arr = gameObject.Find("itemHolder").GetComponent("playerInventory").playerInventory;
function OnTriggerEnter (other : Collider) {
}
var i = 0;
while (i < arr.Length && arr[i] != RemoveItem)
i++;
You cannot remove (or add) items inside a foreach loop.
You misuse GameObject.Find(). This function does not look inside the GameObject hierarchy but looks for the first GameObject with the given name amongst all objects in the scene. You might want to use transform.Find() ins$$anonymous$$d.
Thank you for the tips, very helpful! I am quite new to scripting so I was just trying things. So how exactly can I remove "item_$$anonymous$$ey" from the array?
Answer by andeeeee · Aug 29, 2012 at 03:42 PM
If you are using Unity's Array class then you can use the RemoveAt function. This lets you remove an item by its integer index. You can find the index of the item with the key using a loop like this:-
var i = 0;
while (i < arr.Length && arr[i] != key)
i++;
If you are using a standard .NET array, you can use the following function:-
function RemoveByName(arr: String[], key: String) {
var i = 0;
while (i < arr.Length && arr[i] != key)
i++;
if (i < arr.Length) {
for (var j = i + 1; j < arr.Length; j++) {
arr[j - 1] = arr[j];
}
System.Array.Resize.<String>(arr, arr.Length - 1);
}
}
I am using the Unity Array class. With your mentioned script I still don't know the index for the item "item_$$anonymous$$ey". Please help!
You'll notice the loop in the first code sample I posted looks for the key in the array. The i variable contains the index of the key item if it is present in the array.
Sorry I am completely new to this so I have a hard time understand how this works. I am putting my current script in the first post. At the moment I get a few errors.
Firstly, you need to put the other code inside the OnTriggerEnter function (ie, between the curly braces) for it to be activated by an object entering the trigger. It seems to be mostly O$$anonymous$$ - after the while loop has finished, the i variable will contain the index of the item to remove. You can pass that index to the RemoveAt function to actually take it out of the array.
Answer by Bunny83 · Aug 29, 2012 at 03:32 PM
Well, you didn't tell us what playerInventory is, but i guess it's a List? The Remove function does exactly what you want ;)
I can't show you a full example since i don't know what you actually store in your inventory. This example just uses an "Item" class. First you have to import the System.Collections.Generic namespace at the top of your script like this:
// Unityscript
import System.Collections.Generic;
// UnityScript
var playerInventory : List.<Item>;
To create an instance of a list you would do:
// UnityScript
playerInventory = new List.<Item>();
I'm sorry! I use javascript. playerInventory is an array. When I hit certain boxes, their tags are stored into this array. I only need to now how exactly I can remove a certain element from the array when I only know the name of the element that has to be removed.
That makes no difference. You shouldn't use the Array class at all since it's slow and not type-safe. I add an example in my answer.