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 D3m0nE · Jul 23, 2013 at 04:00 AM · arraylistaddremovegeneric

Remove and Add to List By Name aad

Hello Everyone

i have a 2 List

1 Called : freeSpawnPoints

2 Called : TheNewSpawnPoints

both of them Contain Transforms

..

now.... i want to Add Something from List1 to List2 by using the name of transform

for Example

i have List1 Transform Called "T1"

i want to move "T1" Transform from List1 to List2

Here is my Idea Code :

     freeSpawnPoints.Add(TheNewSpawnPoints.Find("T1"));
 
 
 //AND i need Same way to remove it from the List
 
 TheNewSpawnPoints.Remove("T1");



i hope u got my poing.. and thanks

Comment
Add comment · Show 4
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 Dimling · Jul 23, 2013 at 06:30 AM 0
Share

You can just use a for loop like this. It loops trough the list of objects and get the object with the wanted name, and then just add it to the other list. Works the same way with delete:

 //haven't been tested.
 foreach (Transform t in freeSpawnPoints) 
     {
         if(transform.name.Equals("T1")){
                   TheNewSpawnPoint.Add(t);
             }
     }
 

If you want better performance you should have a look at HashTable maybe. :) Good luck

avatar image D3m0nE · Jul 23, 2013 at 07:17 AM 0
Share

i'm not sure.. but its not working

i also guess u mean this

  if(t.name.Equals("T1")){
 
 //not
 
  if(transform.name.Equals("T1")){

anyway tried both but didnt work

Error :

 InvalidOperationException: Collection was modified; enumeration operation may not execute.
 
 

in this line :

 foreach (Transform t in TheNewSpawnPoints)

. thx for help

avatar image Dimling · Jul 23, 2013 at 07:40 AM 0
Share

Yeah of course. Silly me. Don't have the possibility to test anything here. The Exception is because we are trying to modify the collection while looping trough it.

Look at this stackOverFlow answer. $$anonymous$$ight help you. I think i'm gonna keep away from coding any more without a debugger! :P

http://stackoverflow.com/questions/7340757/c-sharp-list-removing-items-while-looping-iterating

avatar image D3m0nE · Jul 23, 2013 at 08:16 AM 0
Share

easier answer please :) still cant get it ] his answer cant work on Transforms

1 Reply

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

Answer by vividhelix · Jul 23, 2013 at 08:27 AM

Try this:

 TheNewSpawnPoints.RemoveAll(x=>x.name=="T1");

Alternatively, this should work as well:

 var t1 = TheNewSpawnPoints.Find("T1");
 freeSpawnPoints.Add(t1);
 TheNewSpawnPoints.Remove(t1); 

 
Comment
Add comment · Show 1 · 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 D3m0nE · Jul 23, 2013 at 06:38 PM 0
Share

Thanks. the first code working fine

and for second code in C# i dont know what i should change "var" to

any way its works. ty

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

17 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

Related Questions

identify and remove an item from a list 1 Answer

Show Top 10 Of 1 GameType 2 Answers

Iterate through Variables in a Script and adding them to a Generic List 1 Answer

Transform[] syntax error 2 Answers

How to select certain Elements in a list using LINQ-methods? 3 Answers


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