• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
Question by Omelet · Jan 22, 2014 at 10:43 PM · listclassescustomremovecustom class

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!

Comment

People who like this

0 Show 0
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

  • Sort: 
avatar image
Best Answer

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 breakstatement 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.)

Comment
rutter

People who like this

1 Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image rutter · Jan 22, 2014 at 11:52 PM 1
Share

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).

avatar image Omelet · Jan 23, 2014 at 12:15 AM 0
Share

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 minutes 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 minutes 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!

avatar image neonblitzer · Jan 23, 2014 at 12:33 AM 0
Share

@rutter: You're right, I'll note it in the answer.

Unity Answers is in Read-Only mode

Unity Answers content will be migrated to a new Community platform and we are aiming to launch a public beta by June 9. Please note, Unity Answers is now in read-only so we can prepare for the final data migration.

For more information and updates, please read our full announcement thread in the Unity Forum.

Follow this Question

Answers Answers and Comments

19 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Removing Inputs from the Input list 1 Answer

A node in a childnode? 1 Answer

How to remove project from the Project Wizard 6 Answers

List That Won't Remove The Last Two Elements 3 Answers

How to remove elements from string lists? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges