BraniDev
BraniDev

Reputation: 13

How to remove Element from list in Unity (in Inspector, Children of Elements)

I have QuestList which contants scriptable objects. I want Delete Elements when the quest is complete.

In "Element 0" there is another element "Completed Objectives" in the first quest "Quest A" when you start up the game quest is set to 0/1 when you finish objective quest is setup to 1/1 (I need to delete Quests so they don't show up after they completed)

how do i delete Elements in runtime? how i can add them? I been googling around 2 hours to find answer for this...

I try out this but doesn't work.

public void RemoveQuest()
    {
        for(int i = 0; i < completedObjectives.Count; i++)
        {
          if(completedObjectives[i] == questToRemove)
          {
                completedObjectives.Remove(completedObjectives[i]);
          }
        }
    }

picture1

picture2

picture3

Upvotes: 0

Views: 6605

Answers (1)

Andriy Marshalek
Andriy Marshalek

Reputation: 276

You can't modify the collection while looping through it.

In your case you can just use this:

completedObjectives.Remove(questToRemove);

Upvotes: 1

Related Questions