# outline of a grid

Hi everyone,

I am trying to create an outline around my grid. Only the grid can contain one or more empty cells.
Especially at the edges.

The code is supposed to draw a vertical line piece of the height of 1.6f except if a cell on the left of the grid is empty, the edge shifts to the edge on its right.

``````for (float i = -Lignes / 2 - offsetX; i < Lignes / 2; i++)
for (float j = -Colonnes / 2 - offsetY; j < Colonnes / 2; j++)
{
int l = (int)i + (int)Lignes / 2;
int m = (int)j + (int)Colonnes / 2;
if (Grille[l, m] != -1)
{
Vector3 worldPosition = new Vector3(i + centrage, 0f, j + centrage) * 1.6f;
obj = Instantiate(gridCellPrefab, worldPosition, Quaternion.identity);

//         if (Grille[l, m - 1] == -1)           //    Test Left border
{
LineRenderer lineRenderer = obj.AddComponent<LineRenderer>();
lineRenderer.positionCount = 2;
lineRenderer.startWidth = 0.1f;
lineRenderer.endWidth = 0.1f;

Collider cellCollider = obj.GetComponent<Collider>();

Vector3 startPoint = new Vector3((i + Lignes / 2) * 1.6f, 0f, (j + Colonnes / 2 - 0.5f) * 1.6f);
Vector3 endPoint = new Vector3((i + Lignes / 2) * 1.6f, 0f, (j + Colonnes / 2 - 1.5f) * 1.6f);

lineRenderer.SetPosition(0, startPoint);
lineRenderer.SetPosition(1, endPoint);
}
}
}
``````

The problem occurs when I release the following code:

``````if (Grid[l, m - 1] == -1)
``````

Indeed when the code is in comment the grid appears perfectly and lines appear (see image below).
We can see that the lines are shifted to the right and upwards.

And if I leave the condition that tests if there is an empty box on its left, then the grid and the lines disappear.

Normally there should be only one line (a single edge), which shifts according to the empty boxes on the left side.

Do you have any ideas for code to create the left edge (white) or the grid outline?

Thanks to you,

A+

EDIT! I think my previous post was wrong.

I think you need to check if you are using Local or World coordinates in your LineRenderer

While not exactly what you’re doing, it’s actually quite similar. Over here I posted a little script that generates a 3d mesh from a 2d image by simply “extruding” the 2d image and connect the outline with the “other side”. If you played minecraft, essentially how MC renders weapons and tools in the world.

Of course you don’t want to generate a 3d mesh, however the edge detection is essentially the same. You simply walk through your grid cells and for each cell that actually “has content” you check the 4 neighbors. If they are empty, you need an outline at this edge. Of course my script generates a quad that goes into the z direction to give the sprite “depth”. You just need that edge itself.

About the misalignment, like Kurt said, make sure you use local coordinates and have the line renderer on the actual object, so they should share the same local space. Also make sure you have set useWorldSpace to false.

edit
I just remembered that there had been another question regarding detecting the outline of irregular shapes which is a bit more difficult. My solution over there was to search for a starting pixel and then just walk along the edge until you hit the start again or reach a dead end. You can see it in action over here. This is also not strictly related to your case, but may give you some ideas depending on your usecase.

Hello to all,

I don’t understand why the following line of code removes the lines from the left edge.

`````` if (Grille[l, m - 1] != -1)         //   Here is a bug.
``````

And if I put it in comment the lines reappear (only one line). see below:

You can see a defect at the bottom left. Indeed the code does not test if there is an empty box to the left of it. Here is the code:

`````` // I stock the variables here
public static float cellWidth = 1.6f;
public static int gridSize = 8;
public static float gridWidth = cellWidth * gridSize;
public static float gridHeight = gridWidth;

public static float left = -gridWidth / 2f;
public static float top = gridHeight / 2f;
public static float right = left + gridWidth;
public static float bottom = top - gridHeight;

// I create the grid
for (float i = -Lignes / 2 - offsetX; i < Lignes / 2; i++)
for (float j = -Colonnes / 2 - offsetY; j < Colonnes / 2; j++)
{
int l = (int)i + (int)Lignes / 2;
int m = (int)j + (int)Colonnes / 2;
if (Grille[l, m] != -1)
{
Vector3 worldPosition = new Vector3(i + centrage, 0f, j + centrage) * 1.6f;
obj = Instantiate(gridCellPrefab, worldPosition, Quaternion.identity);
}
}

// I test left border
EpaisseurLigne = 0.2f;
for (float i = -Lignes / 2 - offsetX; i < Lignes / 2; i++)
for (float j = -Colonnes / 2 - offsetY; j < Colonnes / 2; j++)
{
int l = (int)i + (int)Lignes / 2;
int m = (int)j + (int)Colonnes / 2;
//           if (Grille[l, m - 1] != -1)         //   Here is a bug.
{

LineRenderer lineRenderer = obj.AddComponent<LineRenderer>();
lineRenderer.positionCount = 2;
lineRenderer.startWidth = 0.2f;
lineRenderer.endWidth = 0.2f;

Vector3 startPoint = new Vector3(left - EpaisseurLigne, 0, top + EpaisseurLigne);
Vector3 endPoint = new Vector3(left - EpaisseurLigne, 0, bottom - EpaisseurLigne);

Collider cellCollider = obj.GetComponent<Collider>();

lineRenderer.SetPosition(0, startPoint);
lineRenderer.SetPosition(1, endPoint);  ;
}
}
``````

Your help is welcome,

A+

Hello to all,

I revised my code and replaced

``````if (Grid[l, m - 1] != -1)
``````

by

``````if (m > 0 && Grid[l, m - 1] != -1)
``````

Unfortunately it doesn’t work, maybe because my Grid is initialized like this.
Indeed I neutralize some cells (maybe the edges):

``````void Start()
{
for (int i = 0; i < Lines; i++)                // Lines = 8;
for (int j = 0; j < Columns; j++)      // Colonnes= 8;
{
Grille[i, j] = 0;
}
Grille[0, 0] = -1;
Grille[7, 7] = -1;
Grille[1, 1] = -1;
Grille[6, 0] = -1;
Grille[6, 6] = -1;
Grille[4, 4] = -1;
Grille[3, 3] = -1;
Grille[4, 4] = -1;
CreateGrid();
}
``````

As you can see, there are missing pieces of lines on the left edges.

Thanks for your help,

A+

So I am guessing the 0 index of the 0 row (the bottom row in screen shot)

0 is a viable index position. Since m needs to be greater than 0. Then for a -1 check them -1ns at 0 don’t get considered.
You are looking for m to be greater than -1 or greater than or equal to 0

Hello to all,
why the following code does not work:

``````public static int col, row = 8;
Vector3 cellPosition = new Vector3(row, 0, col) * 1.6f;
GameObject[,] grid;
GameObject contour;
public Material contourMaterial;
public float contourWidth = 2;

void CreateGrid()
{if (col > 0 && grid[row, col - 1] == null)
{
LineRenderer leftLine = new GameObject("LeftLine").AddComponent<LineRenderer>();

leftLine.transform.SetParent(contour.transform);
leftLine.positionCount = 2;
leftLine.useWorldSpace = false;

leftLine.material = contourMaterial;

leftLine.startWidth = leftLine.endWidth = contourWidth;

Vector3 startPoint = cellPosition + new Vector3(-0.8f, 0, -0.8f);
Vector3 endPoint = cellPosition + new Vector3(-0.8f, 0, 0.8f);
leftLine.SetPosition(0, startPoint);
leftLine.SetPosition(1, endPoint);
}
}
``````

Indeed after compilation nothing appears on the screen (no lines)
I have found this code on the net.

“Does not work” is not useful. If you find yourself writing that, just STOP and delete your entire post, and follow these instructions:

I’ll post it yet again for you:

How to report your problem productively in the Unity3D forums:

http://plbm.com/?p=220

This is the bare minimum of information to report:

• what you want
• what you tried
• what you expected to happen
• what actually happened, log output, variable values, and especially any errors you see
• links to documentation you used to cross-check your work (CRITICAL!!!)

You may edit your post above.

And AGAIN, this never changes either:

Time to start debugging! Here is how you can begin your exciting new debugging adventures:

You must find a way to get the information you need in order to reason about what the problem is.

Once you understand what the problem is, you may begin to reason about a solution to the problem.

What is often happening in these cases is one of the following:

• the code you think is executing is not actually executing at all
• the code is executing far EARLIER or LATER than you think
• the code is executing far LESS OFTEN than you think
• the code is executing far MORE OFTEN than you think
• the code is executing on another GameObject than you think it is
• you’re getting an error or warning and you haven’t noticed it in the console window

To help gain more insight into your problem, I recommend liberally sprinkling `Debug.Log()` statements through your code to display information in realtime.

Doing this should help you answer these types of questions:

• is this code even running? which parts are running? how often does it run? what order does it run in?
• what are the names of the GameObjects or Components involved?
• what are the values of the variables involved? Are they initialized? Are the values reasonable?
• are you meeting ALL the requirements to receive callbacks such as triggers / colliders (review the documentation)

Knowing this information will help you reason about the behavior you are seeing.

You can also supply a second argument to Debug.Log() and when you click the message, it will highlight the object in scene, such as `Debug.Log("Problem!",this);`

If your problem would benefit from in-scene or in-game visualization, Debug.DrawRay() or Debug.DrawLine() can help you visualize things like rays (used in raycasting) or distances.

You can also call Debug.Break() to pause the Editor when certain interesting pieces of code run, and then study the scene manually, looking for all the parts, where they are, what scripts are on them, etc.

You can also call GameObject.CreatePrimitive() to emplace debug-marker-ish objects in the scene at runtime.

You could also just display various important quantities in UI Text elements to watch them change as you play the game.

Visit Google for how to see console output from builds. If you are running a mobile device you can also view the console output. Google for how on your particular mobile target, such as this answer or iOS: https://discussions.unity.com/t/700551 or this answer for Android: https://discussions.unity.com/t/699654

If you are working in VR, it might be useful to make your on onscreen log output, or integrate one from the asset store, so you can see what is happening as you operate your software.

Another useful approach is to temporarily strip out everything besides what is necessary to prove your issue. This can simplify and isolate compounding effects of other items in your scene or prefab.

Here’s an example of putting in a laser-focused Debug.Log() and how that can save you a TON of time wallowing around speculating what might be going wrong:

https://discussions.unity.com/t/839300/3

“When in doubt, print it out!™” - Kurt Dekker (and many others)

Note: the `print()` function is an alias for Debug.Log() provided by the MonoBehaviour class.