rows and columns of a cell in a grid

Hello to all,

I managed to make the following code work:

void CreateGrid()
    {
        float CellX, CellY;


        for (float i = -Lignes / 2, i2 = 0; i < Lignes / 2; i++ , i2++ )
           for (float j = -Colonnes / 2, j2 = 0; j < Colonnes / 2; j++ , j2++ )
            {
                Vector3 worldPosition = new Vector3(i, 0f, j) * 1.6f;
                var obj = Instantiate(gridCellPrefab, worldPosition, Quaternion.identity, transform);

                CellX = i2;
                CellY = j2;

                obj.transform.parent = empty.transform;
                obj.name = "cube" + k;
                k++;
            }
    }

The problem is that I don’t know how to use the variable hit.transform.gameObject.name (numbered with k) (which represents the number of the box that was clicked).
The second problem is CellX only returns the values of the public variable Rows and CellY the value of the public variable Columns.
In short I can’t get the rows and columns of hit.transform.gameObject.name

  • I’m looking for a mathematical formula (see below)
float worldPositionX = ... from the raycast above
worldPositionX -= CenterX ; // where you consider the "zero" line of cells
worldPositionX /= CellXSize ; // central spacing of the cells
int cellX = (int)worldPositionX ;
  • only I don’t understand what it means
float worldPositionX = ... from the above raycast
  • as well as CenterX (can you help me?)

  • I don’t see why you should override the worldPositionX variable.
    On the other hand I assume that CellXSize = 1.6f (grid cell width)

Thanks for your help,

A+

The above code will NOT work because you are simply refusing to understand what has already been written.

Every single question you ask in this post has already been answered above.

I have given you 100% functioning example code and a project

I also explained it.

Spiney has explained it as well.

Since you refuse to understand it, I am not going to retype it.

If I retyped it, you would not understand it AGAIN!

The answer has not changed. Scroll up and read it.

When reading the above posts, it may be helpful to follow this guide:

Step 2. Go back and work through every part of the tutorial again, and this time explain it to your doggie. See how I am doing that in my avatar picture? If you have no dog, explain it to your house plant. If you are unable to explain any part of it, STOP. DO NOT PROCEED. Now go learn how that part works. Read the documentation on the functions involved. Go back to the tutorial and try to figure out WHY they did that. This is the part that takes a LOT of time when you are new. It might take days or weeks to work through a single 5-minute tutorial. Stick with it. You will learn.
Step 2 is the part everybody seems to miss. Without Step 2 you are simply a code-typing monkey and outside of the specific tutorial you did, you will be completely lost. If you want to learn, you MUST do Step 2.

Good luck my friend!