- Home /
creaating a list of object in a for each loop
trying to create a list of objects each includes a couple of ints a couple of strings and a sprite like this
public class fighterKillObject
{
public int universe;
public int killCount;
public string name;
public string info;
public Sprite image;
}
im trying to create this list :
public List<fighterObject.fighterKillObject> universe2Kills;
in a foreach loop based on a group of gameobjects like below(notice the print statement)
fighterObject.fighterKillObject fo;
int index = 0;
foreach (var child in snapshot.Children)
{
print("index " + index);
index++;
int myInt = int.Parse(child.Child("killCount").GetValue(true).ToString());
fo = new fighterObject.fighterKillObject
{
universe = 2,
killCount = myInt,
name = child.Child("name").GetValue(true).ToString(),
info = child.Child("info").GetValue(true).ToString(),
image = universe2Sprites[index]
};
universe2Kills.Add(fo);
}
u2done = true;
killCount(universe2Kills);
}
as of right now the print statement only prints once and the for each loop effectively breaks, but if i comment out the code for creating a new object (this code below)
fo = new fighterObject.fighterKillObject
{
universe = 2,
killCount = myInt,
name = child.Child("name").GetValue(true).ToString(),
info = child.Child("info").GetValue(true).ToString(),
image = universe2Sprites[index]
};
universe2Kills.Add(fo);
}
it prints 6 times (like it should) any one know whats breaking this full code is here https://pastebin.com/N1Bvgh30 and yes i know im doing some awful things in this code ill fix it later lol many thanks
Share what error you are receiving, and oh my god my eyes burn when I look at that code. So many variables, ins$$anonymous$$d of an array oh god.
lol sorry it was a null reference exception it looks like its because im not used to C# and my lists are null
Your answer
![](https://koobas.hobune.stream/wayback/20220612132245im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
A node in a childnode? 1 Answer
[Closed]Help with PhotonNetwork.playerList 2 Answers
For loop does not loop C# 2 Answers
Copy values between two classes in two lists. 1 Answer
Counting active objects in a list 3 Answers