- Home /
Unity adding element to 2D array list
I have an array list chunkData and need to add values with an index using a for loop:
List<int[,]> chunkData = new List<int[,]>();
for (int i=0;i<=x;i++){
for (int j=0;j<=y;j++){
chunkData[i,j] = GetChunkPointData(i,j);
}
}
I am being thrown the error "no overload method for this takes 2 arguments"
GetChunkPointData method cannot take 2 argument i and j..
Can you share your GetChunkPointData method.
Answer by Creatom_Games · Jan 07, 2018 at 06:31 PM
Try using a Hashtable. It's another kind of list which takes a key and a value, the key being the index.
Hashtable chunkData = new Hashtable();
for(int i = 0; i <= x; i++){
for(int j = 0; j <=y; j++){
chunkData.Add(i, j);
}
}
the problem with doing that I discovered is that at the index of i and j I need to get a specific value retrieved by a function; basically, I would need to do chunkData[i][j] = SpecificValue(i,j). How would I implement that here
We cannot know that because we don't know how your specific values are setup. As i've mentioned in my comment above.
Answer by YoucefB · Jan 07, 2018 at 09:44 PM
List<List<int>> chunkData = new List<List<int>>(); // this is how you define a 2d generic List.
for (int i=0;i<=x;i++){
chunkData.Add(new List<int>());
for (int j=0;j<=y;j++){
chunkData[i].Add(GetChunkPointData(i,j));
}
}
Your answer
Follow this Question
Related Questions
How can I move back to Element 0 in an array to reset a loop? 1 Answer
Can't add multiple integers to a List in a Loop! 2 Answers
Why does it jump every second step in this loop? 2 Answers
Why does the decimal point matter in my for loop? 1 Answer
How to make a function that runs a loop x amount of times (Determined by parsed int) 1 Answer