- Home /
Using LINQ within a foreach only runs once
I'm trying to reload the player's items back into their inventory when they load the game. "itemStrings" below is a list of strings of all the item names from when the player saved the game. Here's the code:
void Load(){
List<string> itemStrings = SaveLoad.Load<List<string>>("Inventory");
foreach(string nameString in itemStrings)
{
loadReference = itemDatabase.itemsDatabaseList.Where(obj => obj.name == nameString).First();
bool wasAdded = instance.Add(loadReference);
}
}
For reference, itemDatabase is a reference to an ItemDatabase class that stores every item in the game. loadreference is an Item class that's declared at the beginning of this Inventory class.
What I'm trying to do is search the database for an item whose name matches the string from itemStrings, then take that item and add it to the player's inventory. This 'foreach' works correctly for the first item in the list of itemStrings, but it only runs that one time. So it only loads one item into the inventory, no matter how many were saved previously.
Answer by Nubaa · May 05, 2020 at 08:08 PM
An error in another script was causing the loop to stop running, I'm an idiot.
Your answer
Follow this Question
Related Questions
Logic question, unique path finding system 2 Answers
Foreach loop doesn't run 2 Answers
Foreach's char loop not functioning as expected 1 Answer
Foreach loop inside FileStream 1 Answer
Array Element in Foreach loop 1 Answer