Is it possible to create new variables by incrementing?

So, I have a wall of 2d bricks, and when you click on any of the bricks, it is destroyed and a 3d brick is instantiated on that spot and flies forward. If you click on the flying 3d brick, it then gets destroyed and a new 2d brick needs to emerge in the spot of the original.

 tempRedBrick = Instantiate(prefabBrick, transform.position, transform.rotation);

            static var posBrick: Vector3 = transform.position;
            static var rotBrick: Quaternion = transform.rotation;

Using this code, I get the original position of the 2d brick on the wall, and the new 2d brick is instantiated there. However, it always uses the position of the last 2d brick clicked. I have a static var that increments every time you click on a 2d brick on the wall, I was wondering if there was a way to use this variable in combination with the position and rotation storing variables. This way, the game understands where the 3d brick originally emerged from.

e.g. can I start with something like posBrick0 then the next 2d brick that is clicked is posBrick1, then posBrick2, etc. ?

I don't entirely understand your problem from the description, but just going on your very last sentence, it sounds like you need to use an Array for this.


var bricks = new Array();

function DoBrickStuff() {

tempBrick = Instantiate( prefabBrick, .....etc );


You'll then find that your "bricks" array contains a reference to each brick created, and you can access them like this:


Where "n" is a numeric var containing the index number of the item you want from the array. The index is "zero-based" meaning the first item is zero, the second item is 1, etc.