- Home /
How to acces the value of List of arrays
So I was trying to make a chess game. My initial approach is to evaluate the positions of the board using an array of int that should be of length 2 (x-component and y-component). When it comes to move a piece I create a list of int arrays to store all possible places to move. This works just fine, but when I want to read a position, I try:
public override List<Vector2> PossibleMoves()
{
// I create an empty list that will be filled in the next method CheckMoves
List<int[]> possibleMovesList = new List<int[]>();
// This works fine.
CheckMoves(possibleMovesList);
// HERE COMES THE PROBLEM
Debug.Log("First possible move: (" + possibleMovesList[0][0] + ", " + possibleMovesList[0][1] + ")");
return possibleMovesList;
}
I use possibleMovesList[0][0] since I want to acces the first element of the list (possibleMovesList[0]), and the first element of this array that should be the x-component. (possibleMovesList[0][0]).
But when I try this it shows me the LAST element of the list, not the first one as I expected.
I know that it can be fixed using Vector2 instead of int[] (I actually fixed it already like that), but I'm still curious about how to access this list of arrays elements, and since the positions are integers and they will never use decimals, I think it is more "elegant" to do it like that.
Your answer
Follow this Question
Related Questions
Array sub elements setup question 1 Answer
GameObject to Array/List or whatever fits best 0 Answers
How can you do calculations on two lists? 1 Answer
What do you think of this? List, Vector 3 and Update 1 Answer
A node in a childnode? 1 Answer