Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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
0
Question by getyour411 · Aug 31, 2013 at 06:36 AM · listsremoveat

C# Using Lists and RemoveAt

I'm doing something wrong but as this is my first time working with Lists, I don't know what. My MobSpawner does this

 private void SpawnAllMobs() {
     
     for(int mobcnt = 0; mobcnt < mobCount; mobcnt ++) {
                 
         GameObject mob = Instantiate(mobPrefabs[Random.Range(0,mobPrefabs.Length)],
                 transform.position, Quaternion.identity) as GameObject;
             
         // Get rid of (Clone), setup Parent
         mob.gameObject.name = mob.gameObject.name.Replace("(Clone)","").Trim();
         mob.transform.parent = transform;
         mob.gameObject.GetComponentInChildren<QM_EnemyHealth>().myIndex = mobcnt;
         mobs.Add(mob);
         }
     
 }

and I see my bad guys have a myIndex value of 0,1,2 (where mobCount is 3) - all good there. When bad guy dies it goes into this:

 public void RemoveAMob(int listIndex) {
     mobs.RemoveAt(listIndex);
     
 }

the myIndex value is passed to listIndex. From that, I'm getting an error Argument is out of range Parameter name: index but not every time. If I kill mob(0) and do this RemoveAt(0), does something happen to mob(1) and (2) in the List?

http://wiki.unity3d.com/index.php?title=Which_Kind_Of_Array_Or_Collection_Should_I_Use?

http://unitygems.com/mistakes1/

Comment
Add comment
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

· Add your reply
  • Sort: 
avatar image
3
Best Answer

Answer by syclamoth · Aug 31, 2013 at 06:43 AM

Short answer: yes, when you remove a mob from the list the indices of the rest of the mobs will get messed up because the list is now shorter! Remember, a list is not an array. When you remove an object from the list, it automatically shortens the list.

Why not make things easier for yourself, and let the list manage it for you? Immediately before you destroy the mob, use

 mobs.Remove(curMob);

so that you don't have to worry about keeping track of individual indices.

Comment
Add comment · Show 4 · 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 syclamoth · Aug 31, 2013 at 06:45 AM 0
Share

Better yet, go one step further and use a HashSet ins$$anonymous$$d. If every mob instance is unique, and you don't have to worry about ordering, it will make many common operations much faster.

avatar image getyour411 · Aug 31, 2013 at 06:46 AM 0
Share

This sounds good - cur$$anonymous$$ob (gameObject) shares the same name as all the other mobs in the List, can I assume the List uses a pointer or such to know which gameObject is meant for removal?

avatar image getyour411 · Aug 31, 2013 at 07:11 AM 0
Share

I see through testing that it does not rely on go.name and my mobspawner is now working, thanks syclamoth. I'm new enough to program$$anonymous$$g in general to say that after reading this http://www.dotnetperls.com/hashset I was sufficient lost and put that on my ToLearn list.

avatar image syclamoth · Sep 01, 2013 at 07:33 AM 0
Share

Huh, that page you linked sure looked needlessly complicated! It's really not as hard as it looks, all you really have to do is replace 'List' with 'HashSet', and all the rest of the functionality is the same. All the function calls you need to access a list are the same for a hashset, unless you are trying to manipulate several collections at once (searching for overlapping values, etc), which you are not.

Rule of thumb: hashsets are, in the majority of cases where you are dealing with a group of unique agents, categorically more useful than lists.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

18 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

Related Questions

Remove item from list when clicked on? 1 Answer

How to make an abstract class with a constructor that needs a List of custom objects? 1 Answer

JavaScript multidimensional array editing 1 Answer

Serialization of list in list problem with depth 0 Answers

check if something is on a list by one argument 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