• 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 Brokenarrow · Mar 09, 2016 at 06:46 PM · gameobjectlistcomponentaddremove

Get GameObject from List based on attached script or assigned name

Hi, I was wondering if there was any way to identify an script-created GameObject based on its attached script OR its assigned name in a list. For example:

 newEntry = new GameObject("FirstEntry");
 newEntry.AddComponent<EntryScript>();
 newEntry.GetComponent<EntryScript>().ID = 1;
 entryList.Add(newEntry);
 
 newEntry = new GameObject("SecondEntry");
 newEntry.AddComponent<EntryScript>();
 newEntry.GetComponent<EntryScript>().ID = 2;
 entryList.Add(newEntry);
 
 
 entryList.Remove(?);

If I wanted to remove only FirstEntry, how would I go about that? Im trying to dynamically create a LOT of GameObjects (some different, some duplicates) and save them in lists, but I'm not sure how to identify them after I add them. entryList.Remove(newEntry) logically only removes the last added newEntry.

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

3 Replies

  • Sort: 
avatar image
Best Answer

Answer by RudyTheDev · Mar 09, 2016 at 10:10 PM

What @BackslashOllie posted, but with break; and == (also in descriptive method for cleaner code):

 private void RemoveFirstEntryWithID(int id)
 {
     for (int i = 0; i < entryList.Count; i++)
     {
         if (entryList[i].GetComponent<EntryScript>().ID == id)
         {
             entryList.RemoveAt(i);
             break; // jumps out of the for loop after entry removal
             //i--; would go here if we didn't break
         }
     }
 }

By the way, consider making your entryList to List<EntryScript> entryList instead if it's applicable, so you can do:

 newEntry = new GameObject("FirstEntry").AddComponent<EntryScript>();
 newEntry.ID = 1;
 entryList.Add(newEntry);

 newEntry = new GameObject("SecondEntry").AddComponent<EntryScript>();
 newEntry.ID = 2;
 entryList.Add(newEntry);

And then simply if (entryList[i].ID == id).

Comment

People who like this

0 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 Brokenarrow · Mar 10, 2016 at 01:19 PM 0
Share

All great suggestions! Adding break; was exactly what I needed to get what I want. Thanks for all the help.

avatar image

Answer by BackslashOllie · Mar 09, 2016 at 07:08 PM

Can you not do this?

 for (int i = 0; i < entryList.Count; i++)
 {
     if (entryList[i].GetComponent<EntryScript>().ID = 1)
         entryList.RemoveAt(i);
 }
Comment

People who like this

0 Show 2 · 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 maestroxxx · Mar 09, 2016 at 07:52 PM 0
Share
 if (entryList[i].GetComponent<EntryScript>().ID == 1)

Also run over a reference copy of the list otherwise you might get problem if you remove an item from the list while still iterating over it.

avatar image Brokenarrow maestroxxx · Mar 09, 2016 at 08:49 PM 0
Share

Yes I've been experimenting with a for loop: but I can't get exactly the results I want. I should have been more clear: I want to be able to remove 1 GameObject with a certain ID from the entryList. Thus if I have an entryList with:

FirstEntry (ID = 1) FirstEntry (ID = 1) SecondEntry (ID = 2) FirstEntry (ID = 1)

I want to be able to remove 1 FirstEntry GameObject (for example). With all for/foreach loops I've tried I end up removing all items with a specific ID. Maybe I should first put all items with a certain ID in a seperate list, then remove one and put the remaining back in the original list? Thanks for the suggestion though, I appreciate the help.

avatar image

Answer by MalzanFaren · Mar 10, 2016 at 09:15 AM

Put "break" after the RemoveAt to stop the for loop iterating again if it has performed a removal. For example:

 if (check ID){
      entryList.RemoveAt(i);
      break;
 }

Comment

People who like this

0 Show 0 · 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

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 on June 13. 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

45 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 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

When removing something from a list it says missing 1 Answer

Problems creating new List of type GameObject and adding GameObject to it. 1 Answer

Remove and Add to List By Name aad 1 Answer

Adds infinite # of components, using GameObject.AddComponent. 4 Answers

Component Array 0 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