- Home /
How to get variables from a list of a list of gameobjects?
I have a list that contains multiple lists of GameObjects in it. I am trying to get access to one of the GameObjects variables.
Here is what I am trying to do:
private List<GameObject> row1 = new List<GameObject>(3);
private List<GameObject> row2 = new List<GameObject>(3);
private List<GameObject> row3 = new List<GameObject>(3);
private List<List<GameObject>> winning = new List<List<GameObject>>(3);
void Start () {
row1.AddRange(new GameObject[] { board[0], board[1], board[2] });
row2.AddRange(new GameObject[] { board[3], board[4], board[5] });
row3.AddRange(new GameObject[] { board[6], board[7], board[8] });
winning.AddRange(new List<GameObject>[] { row1, row2, row3});
}
bool CheckForWin()
{
foreach (List<GameObject> possiblity in winning)
{
print(possiblity[0].GetComponent<BoardSpaces>.userID);
}
return false;
}
Comment
Answer by Paulo-Henrique025 · Feb 06, 2013 at 07:15 PM
You need another foreach inside the one you already have.
foreach (List<GameObject> possiblity in winning)
{
foreach(GameOject g in possibility)
{
print(g.GetComponent<BoardSpaces>().userID);
}
}
I tried that but I was still getting an error. What I did was to change all from a GameObject tags to BoardSpaces tags.
Your answer
Follow this Question
Related Questions
C# Trouble testing whether a list of game objects contains a specific game object 1 Answer
Distribute terrain in zones 3 Answers
C# Problem Creating List of Texture2Ds 0 Answers
How can I use foreach and a list of vector3s to reset the positions of all child objects? 1 Answer
null reference exception when I already checked if this was null 1 Answer