Friday, 27 September 2013

Creating Class instances in Unity at runtime

Creating Class instances in Unity at runtime

I want to create X objects in a loop, access and set properties of newly
created objects. I've used the Instantiate method with a passed RigidBody,
like shown in the tutorial, but I need to access the properties of the
script connected to the object, not just it's rigid body.
Here's the method I'm trying to make work:
public void makeGrid(int w = 10, int h = 10)
{
gridWidth = w;
gridHeight = h;
int tileArrayLength = gridWidth * gridHeight;
tileArray = new Tile[tileArrayLength];
for (int i = 0; i < gridWidth; i++)
{
for (int j = 0; j < gridHeight; j++)
{
// add tiles
Tile t = new Tile(); // !!! doesn't work >_<
t.posX = i;
t.posY = j;
t.id = j + 10 * i;
tileArray[t.id] = t;
}
}
}

No comments:

Post a Comment