- Home /
Help removing a Custom Class from a List in UnityScript
Hey folks! I have been Goggling my head off today trying to find out how to remove an item from a list of custom classes (involving UnityScript which is my preferred language of choice). I managed to figure out how to add to the list but removing from that list seems to be an entirely different beast. I think I may need to add another constructor to my custom class but not sure exactly.
The error I am getting with the code below: "Assets/Scripts/BuffInterface.js(21,56): BCE0023: No appropriate version of 'System.Collections.Generic.List.IndexOf(BuffClass)' for the argument list '(String)' was found."
BuffClass.js:
#pragma strict
public class BuffClass {
public var name : String;
public var icon : Texture2D;
public var description : String;
public function BuffClass(newName : String, newIcon : Texture2D, newDescription : String){
name = newName;
icon = newIcon;
description = newDescription;
}
}
BuffInterface.js:
#pragma strict
import System.Collections.Generic;
public var debuffIcon1 : Texture2D;
public var debuffIcon2 : Texture2D;
private var buffs : List.<BuffClass> = new List.<BuffClass>();
function Start () {
buffs.Add( new BuffClass("Poison",debuffIcon1,"Oh noes!"));
buffs.Add( new BuffClass("Radiation", debuffIcon2,"Ouch!"));
}
public function ApplyBuff(name : String, icon : Texture2D, description : String, invoke : String){
buffs.Add( new BuffClass(name,icon,description));
Invoke (invoke,.1f);
}
public function RemoveBuff(name : String){
for(item in buffs){ //NOTE: Thought going through a loop might allow me further access into the class.
if(item.name == name){
var indexOfBuff = buffs.IndexOf(item.name); //NOTE: I am ultimately trying to remove something from the list by its inner class variable name (i.e. via RemoveBuff("Poison") or RemoveBuff("Radiation").)
print(indexOfBuff);
}
}
//var buffToRemove = buffs.IndexOf(BuffClass(name,icon,description));
//print(buffToRemove);
//buffs.Remove(buffToRemove);
}
Thanks for any and all help!
Answer by neonblitzer · Jan 22, 2014 at 10:55 PM
The IndexOf function wants a reference to the object as an argument. You'll get the correct index with this:
var indexOfBuff = buffs.IndexOf(item);
But you're making it more difficult than you have to. If I understood you correctly, you can squeeze the function into this:
for (var i = buffs.length - 1; i >= 0; i--) { // reverse iteration
if (buffs[i].name == name) {
buffs.RemoveAt(i);
break;
}
}
That's how I'd do it.
BTW, you can't modify a collection (list, array...) inside a foreach loop ( for (x in y)
syntax).
EDIT:
If there are multiple buffs with the same name, the code above only removes the first match. To remove them all, just delete the break
statement from the loop body.
(This is why the code iterates over the list in reverse – you can remove items without having to worry about jumping over any by accident.)
One caveat: important to be clear if you're removing the first match only, or all matches. That code looks to do the latter, but could miss matches if they're directly adjacent in the list (ie: remove index 4, and the entry that was at index 5 will be skipped).
Yes. I haven't tried your code yet neonblitzer but, I would be interested to do so for the sake of learning. Thanks for the help. Having a loop there was over-complicating things but I just had to attempt to go at it from a different angle (because I thought if I can retrieve all the info easily with foreach loops then that might be a good enough solution until I knew more about UnityScript). As for my problem, I managed to get it solved just a few $$anonymous$$utes ago. For anyone from the future having this same problem (and want to go at without using loops):
similar Unity Answers question
Although I saw it, I couldn't make sense of it and it just looked rather strange (plus I wasn't using transforms but a custom class). At the time I thought "why would you define a blank function in the middle of finding an index" and couldn't really de-crypt it until I ran across this just a couple $$anonymous$$utes ago (that involved a custom class specifically):
var index = anotherList.FindIndex(function(entry) entry.someValue == something);
Which is exactly what I wanted to accomplish in the first place. Sorry if my script was a giant mess and I didn't make it clear what I was trying to accomplish. That example code above came from Unity Gems - Common Gotchas #1. So I managed to get up and running with this:
var indexOfBuff = buffs.FindIndex(function(item : BuffClass) item.name == buffname);
I tested it and it appears to work perfectly in getting the index, now I just need to add the Remove(indexOfBuff) and it should be what I was looking to do. Thanks Neon and Rutter! I really appreciate the help!
Your answer
![](https://koobas.hobune.stream/wayback/20220613131155im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Removing Inputs from the Input list 1 Answer
A node in a childnode? 1 Answer
Remove and Add to List By Name aad 1 Answer
In a tile-based game, best way to select multiple tiles via dragging? 1 Answer
Find specific prefab in a list 2 Answers