- Home /
How to store a Transform[,] grid in an ArrayList?
I am new to Unity game dev and trying to expand my knowledge of data structs. I'm building a match-3 game, and I currently have a multi-dimensional ArrayList holding transform coordinates on a grid of x width and y height. I have game objects tied to these coordinates in world space, with the grid syncing the data back in game space. I am attempting to push these coordinates into the nested list (familyList) to be deleted pending 3 or more units being added to familyArrayList (seen below).
Here's the abridged code:
private static Transform[,] grid = new Transform[width, height];
public static ArrayList familyArrayList = new ArrayList();
// Go through game grid line by line and add game objects from game space into family ArrayList
for (int y = height - 1; y >= 0; y--)
{
for (int x = 0; x < width; x++)
{
if (grid[x, y] != null)
{
// Add grid[x, y] to familyArrayList
familyArrayList.Add(grid[x, y]);
Debug.Log(familyArrayList[0]);
Output:
Green Unit(Clone) (UnityEngine.Transform)
How can I get familyArrayList to properly story the grid coordinates and not the game object the grid coordinates are referencing? i.e. familyArrayList[0] == '3,4' Should I instead use a nested list? At a later point in the code, I want to clear the game object from the board in world space as well as set the grid[x,y] to null:
// Loop through all family array and destroy game objects
foreach (var x in familyArrayList)
{
Destroy(familyArrayList(x).gameObject);
}
// Loop through all family array and set their values to null in grid as well
foreach (var x in familyArrayList)
{
familyArrayList(x) = null;
}
But I get errors like "Argument 1: cannot convert from 'object' to 'int'" and "Non-invocable member 'Piece.familyArrayList' cannot be used like a method" which I can imagine are explained by my above problem of not storing the grid coordinates properly and instead storing the referenced game object. I'm truly stumped on this problem and thank anybody in advance who can point me in the right direction to solving this.
Hi there I am a little confused at what you are trying to do .. if you are trying to store a collection of grid coordinates why not just store them in a list of Vector3/2s?
That said.
Could you rewrite your question explaining just the problem you are trying to solve as there is a lot of noise around it which will affect its "searchability" for people looking for similar answers in the future.
Answer by logicandchaos · Feb 02, 2020 at 06:46 PM
In your ArraryList you are adding transforms to it so it will store transforms, if you want it to store positions you have to add that to it instead, familyArrayList.Add(grid[x, y]); should be familyArrayList.Add(grid[x, y].position); it looks like you are not just new to unity but to C# as well you need to learn the C# basics about variable types. If you use List instead of ArrayList you can strong type it, public List family; or public List family; depending on what you want. Also post your errors and code or people will not be able to help you, the error tells you what line the error occurred on and the type of error.
if didn't print my in list declaration should be:
public List<Transform> family;
or
public List<Vector3> family;
also if you want to convert a 1d array into a 2d array you have to divide by the number of columns.
Answer by StefanJo · Feb 02, 2020 at 06:41 PM
ArrayList
is untyped so in order to achieve what you are trying to the best option is to use List inside System.Collections.Generic
:
List<Transform> familyList
Also you don't have to iterate over all the elements in the list in order to clear the list, you can just use familyList.Clear();
.