- Home /
How do you store instantiated tiles using a coordinate system?
Hi community. I'm trying to produce a program that generates a gridmap, where each instantiated tile has a script attached generating data unique to each tile. I'm struggling to understand how I can define the name of these instantiated tiles using a coordinate system, shown in the code below. I was attempting to use lists with a row and column value, though it appears lists cannot be used as variable names.
The code (The segment between the two for loops is place where this needs to happen):
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GridGeneration : MonoBehaviour
{
public int rows = 10;
public int cols = 10;
public float tileSize = 5f;
void Start()
{
generateGrid();
}
void generateGrid()
{
GameObject referenceTile = (GameObject)Instantiate(Resources.Load("blankTile"));
for (int row = 0; row < rows; row++)
{
for (int col = 0; col < cols; col++)
{
List<int> tileCoordinates = new List<int> { row, col };
GameObject tile = (GameObject)Instantiate(referenceTile, transform);
tile.AddComponent<tileData>();
Debug.Log(tile);
float posX = col * tileSize;
float posY = row * tileSize;
}
}
Destroy(referenceTile);
float gridWidth = cols * tileSize;
float gridHeight = rows * tileSize;
transform.position = new Vector2((gridWidth - tileSize) / -2, (gridHeight - tileSize) / 2);
}
}
Thanks for your time!
Answer by SuperCats14 · Jan 07, 2021 at 03:48 AM
Hi. You can use GameObject[,] to store the gameobject in a coordinate-like way.
GameObject[,] tiles;
int maxX;
int maxY;
void Start()
{
tiles = new GameObject[maxX, maxY];
}
for (int x = 0; x < maxX; x++)
{
for(int y = 0; y < maxY; y++)
{
GameObejct tileObject = Instantiate(tile, somePositionIDK, someRotationIDK);
tiles[x, y] = tileObject;
}
}
Hi @SuperCats14 . I'm getting a null reference exception error on my equivalent of the "tiles[x, y]" line.
The tile variable game object declaration:
public GameObject[,] tile;
The assignment of the game object within the loop:
GameObject referenceTile = (GameObject)Instantiate(Resources.Load("blankTile"));
for (int row = 0; row < rows; row++)
{
for (int col = 0; col < cols; col++)
{
GameObject rawTile = (GameObject)Instantiate(referenceTile, transform);
rawTile.AddComponent<tileData>();
tile[col, row] = rawTile;
Debug.Log(tile);
float posX = col * tileSize;
float posY = row * tileSize;
}
}
Sorry if the solution to this is obvious, I'm fairly new to unity.
Hi @$$anonymous$$_terrarian. Did you declare tiles in the Start function? If not do it like this: void Start() { tiles = new GameObject[maxX, maxY]; } I hope this fixes your problem.
Hi @SuperCats14 . I still cannot work out the issue, as I still receive the null reference error. is there are term for the usage of this GameObject list/array so I can search for online documentation?
Answer by CmdrZin · Jan 07, 2021 at 06:26 AM
You could store the Tiles in a Dictionary. Use a string as a key.
Dictionary<string, GameObject> tiles = new Dictionary<string, GameObject>();
string key = "r" + row.ToString() + "c" + col.ToString;
tiles[key] = rawTile;
You can now get a tile back by generating a key knowing the row,col of the tile you need. The key could also be stored as the tile name in the TileData script.
Another note. You could attach the TileData script to the perfab blankTile so it's there when you use it to instantiate.
Thanks @CmdrZin for another great method. Only downside to this is that you need to format back and forth between strings and integers in order to work with relative coordinates. (I'm trying to program $$anonymous$$esweeper for reference.)