- Home /
Remove() in List first works but then it doesn't
Hello Internet, I have a big problem. I have a bunch of codes and classes that handles 2D objects as blocks in serialization cause my game is a 2D sanbox game and I have to have a functional world selection and the list of block ID's (contains float[] for Vector3 and Item Type for Inventory and Block kind) but the problem is that when I call Remove in the list like blocks.Remove(id), it sometimes doesn't work. I have tried so much solution in my head but it never works. Can you help me cause if you can, That means the world to me so thanks
Anyway here is my one Code:
public void RemoveBlock(BlockID id)
{
//Debug.Log(blocks.Find(b => b.pos.Equals(id.pos)));
if (blocks.Contains(id))
{
Debug.Log(id.blockID.type);
}
blocks.Remove(id);
}
`
can you please edit your post to contain only the necessary information? It seems to me like there is a lot of code that is not connected to your question. If it is please outline why.
Also can you please explain what you mean by "It does not work"?(this is mostly a really bad way to describe a problem) Is there an error? If not what happens instead? How do you see what happens/doesn't happen? Be specific here and add details.
Answer by aqquan2017 · Oct 14, 2021 at 02:18 AM
Move this line "blocks.Remove(id);" inside your if statement..It need to be check exsist before you remove.