How do I save a 2DArray of GameObjects

eagleeyez 2 years ago updated by Lazlo Bonin (Lead Developer) 2 years ago 2

How do I save a 2DArray of GameObjects

In the following graph it will create this Grid out of a tile prefab.

You can replace the square prefab with a cube or sphere Prefab in the graph to test this. You only need a Flow Machine on a game object in your scene to create this.

A script called Tile.cs should also be in your project. just create c# script and call it Tile

Now I want to save each created GameObject to a 2D Array at the index of the For Loops.

How do I do that?

Hier is the script that creates this and saves to a 2d array

using UnityEngine;
using System.Collections;

public class Board : MonoBehaviour {
    public int width;
    public int height;
    public GameObject tilePrefab;
    Tile[,] m_allTiles;
    void Start ()
        m_allTiles = new Tile[width,height];
    void SetupTiles()
        for (int i = 0; i < width; i++)
            for (int j = 0; j < height; j++)
                GameObject tile = Instantiate (tilePrefab, new Vector3(i, j, 0), Quaternion.identity) as GameObject;
                tile.name = "Tile (" + i + "," + j + ")";
                m_allTiles [i,j] = tile.GetComponent<Tile>();
                tile.transform.parent = transform;

Bolt Version:
Unity Version:
Scripting Backend:
.NET Version (API Compatibility Level):

No 2d arrays by default in Bolt, but I'm pretty sure you could create a custom array holder, and a set and get as all macros. You probably could use dictionary's for this. A base dictionary, with X and Y as two item keys,  then the value is another dictionary with the int database selection as the key, and then your actual value as that value. Once you make your macros though, you could use them as a type.

Interesting idea though. I have 3 macro sets I'm almost on the verge of finishing. Maybe I'll give this a crack right after. That would be pretty useful. For now your probably need to reference a  2d array type of script if you don't do what I suggest above.

My best guess there.


Instead of a 2D array, how about using nested lists? 

One "parent" list that holds all columns for example, and multiple "child" lists for each item in the column.

You can then use two chained Get List Item units to get the item, the first one for X coordinate and the second one for Y coordinate.