# Instantiating a grid, Scripting Problems

Okay, so I just started learning Unity3D a couple of weeks ago and for my first project I decided to make a remake of Konami's old arcade game Frogger. I'm having trouble with my initialization script. Basically, as you probably know, Frogger moves accross a grid in this game. I figured the easiast way to do this was to write a script to instatiate a grid object at every 1 unit in the X and Z direction for a certain size. That's a pretty poor explanationg but basically I decided to make a grid. each square in the grid could then be set up as a waypoint for the player in the locomotion script. However, I'm having some strange problems with my code. For some reason it's creating duplicates of all the points in the grid and also having them offset by 1. For example, if my X position variable is 0, it for some reason spawns the object at x=1. below is my script.

``````var GridXMax : int;
var GridZMax : int;
var GridXMin : int;
var GridZMin : int;
var tile : Transform;
private var placeX:int;
private var placeZ:int;
var i : int;
private var positionX:int;
private var positionZ:int;

function Start () {
i=0; //counting variable
placeX = GridXMax - GridXMin; //finds the width of the grid
Debug.Log("grid will be "+ placeX + " coords wide");

placeZ = GridZMax - GridZMin; // finds the depth of the grid
Debug.Log("grid will be " + placeZ +" coords long");

while(i<placeX*placeZ)
{
positionX = ((i%placeX)); // finds the x position of the tile
positionZ = Mathf.FloorToInt((i)/placeX); //finds the z position of the tile
Debug.Log(positionX+", "+positionZ);
Debug.Log(i);
Debug.Log("n/");
Instantiate(tile, Vector3(positionX, 0, positionZ), Quaternion.identity); //spawns a tile at positionX and positionZ location, or rather is supposed to
tile.name = "Tile " + positionX + ", "+ positionZ; // names the newly spawned tile
i++;
}
}

``````

I'm new to programming but I have done a lot of reading in the past few weeks and don't mind doing more, so if you see something wrong or off other then the problem I'm currently having I would very much appreciate links to some pages to read. I've also done my best to comment on my code I hope it is adequate. Thanks in advance!

I see it's your first question, welcome to the site! There are quite a few things to say about your script:

First of all, a bit of general scripting advice. You're using a while loop with a certain start value that you are then incrementing inside the loop. This is what for loops (as opposed to while loops) are invented for.Simply use

``````for(i=0;i<placeX*placeZ;i++) {...}

``````

This will start i at 0, run the loop if it's smaller then placeX*placeZ and after each run increment i by one.

Appart from that your script looks very good, especially for a beginner. It's also hard to find the error, but I think it's the way you are using Instantiate. It should be

``````var clone : Object = (Instantiate (original : Object, position : Vector3, rotation : Quaternion)

``````

But here the original : Object you are cloning is defined at the top of your function as

``````var tile : Transform;

``````

Which makes no sense. Turn it into var tilePrefab : GameObject; and drag 'n drop the prefab onto it in the inspector.

The way you currently have it set up is you just clone the prefab, but you never assign a variable to contain this clone. You need to realize that when you use tile.name (after you fix it to be a GameObject, not a Transform) you are referencing the name of the original tile's prefab and changing it's name. You are not referencing the clone. To do this don't just run instantiate (a function that returns a clone) but have a variable be that clone

``````var clone = Instantiate(...);

``````

now when you say clone.name you reference the name of the latest spawn clone. This connection however get's broken every time the loop is run, because the variable clone will contain the newest clone, not one of the older ones. To store references to all of them - which might turn out to be very important if you want to check if you are on any of the tiles:

``````(if (transform.position == any_of_the_tiles.transform.position) Debug.Log("I am on tile.."))

``````

For this check out how to set up arrays. But that's a bit more advanced if you only just started scripting so start with just following the first few things I said and save this for when all that works.

Good luck!