Hey there. I have a system set up that creates an array of objects that forms a grid. I’m trying to make the grid start on the position of the gameObject that it’s attached to, but I’m having trouble referring to the gameObject. Here’s my code.
using UnityEngine;
using System.Collections;
public class GridCreator : MonoBehaviour
{
public int width = 10;
public int height = 10;
public Cell[,] cells;
public GameObject unit;
public class Cell
{
public Vector3 pos;
public int sizeMod = 1;
public Cell(int x, int y)
{
pos = new Vector3((x*sizeMod)+gameObject.transform.position.x,0,(y*sizeMod)+gameObject.transform.position.y);
}
}
void Awake ()
{
cells = new Cell[width, height];
for (int y = 0; y < height; ++y)
{
for (int x = 0; x < width; ++x)
{
cells[x,y] = new Cell(x, y);
GameObject newunit = Instantiate(unit, cells[x,y].pos, Quaternion.identity) as GameObject;
}
}
}
}
The grid works and instantiates Units correctly if I take the additives (+gameObject.transform.position.x/y) out of the equation here
pos = new Vector3((x*sizeMod)+gameObject.transform.position.x,0,(y*sizeMod)+gameObject.transform.position.y);
But when I leave them in I get this error
Cannot access a nonstatic member of outer type
UnityEngine.Component' via nested type
GridCreator.Cell’
Am I trying to access the variable incorrectly? Not sure what I’m doing wrong. Thanks for any help.