- Home /
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.
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)
.
All great suggestions! Adding break;
was exactly what I needed to get what I want. Thanks for all the help.
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);
}
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.
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.
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;
}