Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 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
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

3 Replies

· Add your reply
  • Sort: 
avatar image
0
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
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 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
0

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
Add comment · 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. $$anonymous$$aybe 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
0

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

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

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

Related Questions

Find specific prefab in a list 2 Answers

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

How to add and sort a list of gameobjects by tag? 2 Answers

can add to a list but not remove 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