having a object create itself within the constraints of another objects size

I’m trying to build a level with ‘stages’ or zones that the players can play within. I’ve still learning and I followed a grid system tutorial that works quite well with object snapping and grid resizing, but I’ve run into this issue in the past of wanted something to be created or stuck within the constraints of another object, and I have no idea how to return the actually world space location (length/width for example) and use them as variables.

Here is the grid system, it’s simple.

public class Grid : MonoBehaviour
{
    [SerializeField]
    private float size = 1f;

    // Start is called before the first frame update
    public Vector3 GetNearestPointOnGrid(Vector3 position)
    {
        position -= transform.position;
        int xCount = Mathf.RoundToInt(position.x / size);
        int yCount = Mathf.RoundToInt(position.y / size);
        int zCount = Mathf.RoundToInt(position.z / size);
        Vector3 result = new Vector3(
            (float)xCount * size,
            0f,
            (float)zCount * size);
        result += transform.position;
        return result;
    }
    private void OnDrawGizmos()
    {
        Gizmos.color = Color.white;
        for (float x = 2; x < 27; x += size)
        {
            for (float z = 2; z < 27; z += size)
            {
                var point = GetNearestPointOnGrid(new Vector3(x, 1f, z));
                Gizmos.DrawCube(point, new Vector3(1.0f, 0.5f, 1f));
            }
        }
    }
}

under the draw grizmo’s, manually change the location of my playing field to see the grid. I understand the grid is drawn globally almost, where it’s actually a fairly massive object.

Lets say I have a cube, that’s 25,1,25. (25 units wide and 25 units long). If I move this game object, how would I contain the grid within it, making it possible to spawn this cube anywhere and have a building grid on it?

I’m not sure I know your exact intentions from your explanation in your question, but I’m going to take a stab at it. So, assuming you have one cube of 25,1,25 in size and you know how you want to space the grid above or within. Let’s say for argument sake, you want a grid of cubes to lay on top like you have in your picture. Let’s assume that your spacing of the cubes within the grid is 1 unit in each direction. So, you have a grid of 25 x 25 cubes on top of your large cube that stretches the width and length of the grid. On the script that is attached to the large cube, you can store the grid positions relative to wherever it is in world space:

int columns = 25;
int rows = 25;
float spacing = 1; //leave 1 unit between each grid object
Vector3 startPos = new Vector3(gameObject.transform.position.x - (gameObject.transform.lossyScale.x * 0.5f + (spacing * 0.5f)), gameObject.transform.position.y + gameObject.transform.lossyScale.y, gameObject.trabsform.position.z - (gameObject.transform.lossyScale.z * 0.5f + (spacing * 0.5f)));
Vector3[,] grid = new Vector3[columns, rows];

for(int i = 0, i < columns, i++){
for(int j= 0, j < rows, j++){
Vector3[ i, j] = new Vector3(startPos.x + (i * spacing), startPos.y, startPos.z + (j * spacing));
//you can instantiate a grid cube at this position here to build the physical grid 
}
}

The above will build a new grid based on the location of the game object and its size. Now I can make the grid at the start of the game or if I move the large cube at some point of the game, just have to run this code, and the grid is the exact same size and in the same location in relationship to the large parent cube.