Drawing the grid

Hey! Ive made one script class called “Square” in C#, which got following constructor:

public Square(float x, float y, float width,float length, UnityEngine.GameObject go)

And in the grid class, i am dividing the terrain into square objects and taking them in a Square[ , ] array.
Everything works well, when i test in debug.log the array contains all the squares of the terrain. The problem is when i want it drawn on the terrain as a grid.

I was planning to use lineRenderer to draw the grid so in the Start() method i got the following:

               lineRenderer = gameObject.AddComponent<LineRenderer>();
		lineRenderer.SetVertexCount(5);
		if(lineRenderer == null)
		{
			Debug.Log ("null lineRenderer");
		}else {Debug.Log("lineRenderer");}

Ive tried the following in the Update() method, but it doesnt work:

void Update()
	{
		for(int i=0; i<mXCells; i++)
		{
			for(int j=0; j<mYCells; j++)
			{
				Vector3 pos0 = new Vector3(mSquare[i,j].getX(), zOffset, mSquare[i,j].getY());
				Vector3 pos1 =  new Vector3(mSquare[i,j].getXPlus(), zOffset, mSquare[i,j].getY() );
				Vector3 pos2 = new Vector3(mSquare[i,j].getXPlus(), zOffset, mSquare[i,j].getYPlus() );
				Vector3 pos3 = new Vector3(mSquare[i,j].getX(), zOffset, mSquare[i,j].getYPlus() );
				
				lineRenderer.SetWidth(10,10);
				lineRenderer.SetColors(Color.red, Color.red);
				
				lineRenderer.SetPosition(0, pos0);
				lineRenderer.SetPosition(1, pos1);
				lineRenderer.SetPosition(2, pos2);
				lineRenderer.SetPosition(3, pos3);
				lineRenderer.SetPosition(4, pos0);
			}
		}
		Debug.Log ("Rendered Line");
		
	}

I cant work out whats wrong, why the lines of all the squares arent drawn on the terrain so it becomes a grid. So it would be greate if someone got an idea of whats wrong.
Because it works if i just want the square/tile that the mouse is above to drawn by having this in the Update() method:

function Update () 
{

	////////////////////////////////////////////////////////////////////////////////////////////////////
	//INEFFICIENT, CONSTANT POLLING, BUT WORKS
	///////////////////////////////////////////////////////////////////////////////////////////////////////
	//Debug.Log("Mouse is over the terrain");

	//GET THE MOUSE POSITION
	var mousePos : Vector3 = Input.mousePosition;

	//CREATE A RAY AT THE MOUSE POSITION
	var ray : Ray = mCamera.ScreenPointToRay (mousePos);
	var hit : RaycastHit;
		
	//WHEN THE RAY IS CAST
	if (Physics.Raycast (ray, hit)) 
	{
		//STORE THE POINT WHERE IT HITS
		var hitPoint : Vector3 = hit.point;

		//SEARCHES THROUGH THE CELLS ON THE X AXIS
		for (var i = 0; i < mXCells; i++)
		{
			//SEARCHES THROUGH THE CELLS ON THE Y AXIS
			for (var j = 0; j < mYCells; j++)
			{
				//IF THE HIT POINT OF THE RAY IS BETWEEN THE BOUNDS OF A TILE
				 (
				( hitPoint.x > mTiles[i,j].GetX() ) 
				( hitPoint.z > mTiles[i,j].GetY() ) 
			 	( hitPoint.x < ( mTiles[i,j].GetX() + mTiles[i,j].GetWidth() ) ) 
				( hitPoint.z < ( mTiles[i,j].GetY() + mTiles[i,j].GetLength() ) ) )
				{///checks through all the tiles to see if the ray casted is between the four sides of the tile

						var pos0 : Vector3 = Vector3( mTiles[i,j].GetX(), zOffset, mTiles[i,j].GetY() );
						var pos1 : Vector3 = Vector3( mTiles[i,j].GetXPlus(), zOffset, mTiles[i,j].GetY() );
						var pos2 : Vector3 = Vector3( mTiles[i,j].GetXPlus(), zOffset, mTiles[i,j].GetYPlus() );
						var pos3 : Vector3 = Vector3( mTiles[i,j].GetX(), zOffset, mTiles[i,j].GetYPlus() );
						
						lineRenderer.SetWidth(10,10);
						lineRenderer.SetColors(Color.blue, Color.blue);
	
						//set the lines being drawn
						lineRenderer.SetPosition(0, pos0);
						lineRenderer.SetPosition(1, pos1);
						lineRenderer.SetPosition(2, pos2);
						lineRenderer.SetPosition(3, pos3);
						lineRenderer.SetPosition(4, pos0);	
						
					}		
				}
			} 
		}
	}

Then it all works, then the tile where the mouse points is drawn, but as fast as i remove the ray if tests because i want all the tiles being drawn all the time, then it wont work:S

-HeadBlast, trying making a ladder game:)

The LineRenderer is not very good for such tasks. It’s good if you need a simple way to draw a line or two, but for larger grids it causes more trouble and wastes more ressources than it’s worth. If you want to do it yourself I would recommend you to look into the GL class:

It says that GL needs Unity Pro, but that’s not true, it works with the indy license as well and it will be officially free in Unity 4. Its pretty tricky though. You can use Grid Framework (shameless plugging ^^) which will take care of it automatically. Just create your grid in the editor, set the flag, choose your colours and you’re good to go. You can set the width of the lines as well. If you need more fancy lines, not just plain one-coloured lines, then you might want to take a look at Vectrosity. Grid Framework has even Vectrosity support, you just set up your grid and then you can get all the points properly formatted for use with Vectrosity with one line of code.

Of course that’s assuming that your grid is flat, not on top of some sort of 3D terrain with mountains and stuff. Here are two videos of Grid Framework (it has already been a few releases since then, you can now set size and redering range)

If you really want to use the LineRenderer, have you made sure there is no other renderer (like MeshRenderer) attached to the object that carries the Linerenderer?

Thanks for the reply, i will look into the GL class:)
Tried buying Grid Framework but didnt work on the asset store, maybe cuz i have the free unity version?