Hello. This is a script I found in the unity manuel that instantiates objects in a grid. How can I make it so that after the objects are instantiated in the grid, the grid can move as a whole, while the individual objects are still in the grid formation? I have tried attaching this script to a child object and then moving its parent, but the grid stays in the same place, is there anyway to do what I want? Thanks
var prefab : GameObject;
var gridX = 5;
var gridY = 5;
var spacing = 2.0;
function Start () {
for (var y = 0; y < gridY; y++) {
for (var x=0;x<gridX;x++) {
var pos = Vector3 (x, 0, y) * spacing;
Instantiate(prefab, pos, Quaternion.identity);
}
}
}
Of course there is 
In this particular case, the solution is probably to make the newly instantiated objects children of another object (after which you can move them as a group by moving the parent object).
You can make one object the child of another via code as follows:
childObject.transform.parent = parentObject.transform;
Possibly dumb question, does that bit of code have to be on the objects I want to instantiate, or can it be in the script that is doing the instantiating?
It could be either, but it’d probably make the most sense for it to be associated with the script that’s doing the instantiating.
For example, in the code you posted, that line of code could go right after the line of code that calls Instantiate(). ‘childObject’ would be the object that was just instantiated (you’d have to modify the line of code that calls Instantiate() so that the return value is cached), and ‘parentObject’ would be whatever object you wanted to be the parent (if you want the object that’s ‘doing the instantiating’ to be the parent, ‘parentObject’ would just be ‘gameObject’).
I tried to make those changes, I am getting no errors and It is not working the way I want it to (the parent object moves and yet the grid full of instantiated objects stays in the same place).
var prefab : GameObject;
var gridX = 5;
var gridY = 5;
var spacing = 2.0;
function Start () {
for (var y = 0; y < gridY; y++) {
for (var x=0;x<gridX;x++) {
var pos = Vector3 (x, 0, y) * spacing;
Instantiate(prefab, pos, Quaternion.identity);
prefab.transform.parent = gameObject.transform;
}
}
}
You’re working with the prefab from which the clone is instantiated, but it’s the clone itself that you want to be working with. (You can access the clone via the return value of Instantiate().)
How do I access the return value of Instantiate?
The same way you access the return value of any function.
I’m not sure what it’d look like in UnityScript, but here’s a guess:
var newObject : GameObject = Instantiate(prefab, pos, Quaternion.identity);
newObject.transform.parent = gameObject.transform;
That did the trick, thanks 