- Home /
Create a grid of transform instances
I really need to do this, but im pretty new programing so i dont know where begin. What i need is create a grid with colums and rows to instantiate a series of prefabs on scene on determinate distance, and be able to put and change them within the inspector. I was thinking about some loop var, but i cant figure how to do it. Aprecciate so much your help.
Answer by save · Sep 02, 2011 at 01:13 PM
The Manual page on Instantiate has sample code how to do this.
One only problem, this is good to instantiate the same object, do you know how to make it to instantiate prefabs from an array?? Thanks again
What does the array look like? Do you have different objects for every grid tile, objects that repeat or objects that is defined by you where they should be? How many objects are there?
Basically just bring that array (if you have it constructed) into the for-loop and go wild with it (if(objArray[i]), if(i%5) and so forth). :-)
they are prefabs that make the scenary of the game, every one is put in a number on the array, 1, 2, 3, so on, so if you change a prefab you can modify the scenary, so are some prefabs that are repeated and some that have no repetition. The array looks like the drawing i put up, its a x*y array. I can do this by hand, but i think it would be nicer to find out a code to instantiate prefabs on the inspector in an automatic way.
Answer by NE07HEKIN6 · Sep 02, 2011 at 02:25 PM
I already did it, was simple after all, i post it in case someone needs it
var cube : Transform[];
function Start () {
for (var y = 0; y < 5; y++)
{ for (var x = 0; x < 5; x++)
{ for (var z = 0; z < x*y; z++) { var cube = Instantiate(cube[z], Vector3 (x, y, 0), Quaternion.identity); } } } }
Thanks for sharing. This might answer my need. I am using instantiate prefab, but I am not sure if it can control the texture as flexible as I wanted to be.
Your answer
