# How can i keep a constant grid position relative to transform.ChildCount

I need to manage the index positions in a GridLayoutGroup and keep it consistent with the childCount of the group it belongs to.

The Grid has a fixed ConstraintCount(3) and can be increased or decreased by 3 (it adds a row). The index can also be modified (+1, -1) so that its value within the transform group changes. I initially wrote the code

        destTank = top.transform.childCount - 2;


which works well, it keeps my index destTank  always to be on the top/middle position of the grid even when the grid changes, but it doesn't take in consideration the index variations. as result an index of 9 in a 3x3 grid will become 5 in a 3x2 grid but i need it to be 6. In other terms if the position on the grid is, let's say, on the top/left corner of the grid it should also be in the top/left corner in a smaller grid, not in the top/center position.

to solve this i wrote something like that

        destTank = top.transform.childCount - (top.transform.childCount - destTank);


but it's not working because the value of destTank is always changing and gives me weird results. I tried a lot of different combinations of values, even tried to divide the childCount by the contraintCount to obtain a ratio (1,2,3) corresponding to the rows number and use it somehow as a delta to obtain my index but nothing worked. Maybe I'm just messing up the syntax when writing the math operations. Anyone can help me solve the puzzle? Here's the full code:

    private void Positions()
{
var rows = top.transform.childCount - 3;

destTank = top.transform.childCount - (top.transform.childCount - destTank);
destHealer = center.transform.childCount - 2; // (center.transform.childCount / GLG_center.constraintCount);
destDpsMR = right.transform.childCount - 1;
destDpsML = left.transform.childCount - 1;
destDpsRR = bottomR.transform.childCount - 1;
destDpsRL = bottomL.transform.childCount - 1;

Debug.Log(top.transform.childCount);
}

private void PositionsPlus()
{
if (currentGroupIndex == 0)
{
destTank = (destTank + 1) % top.transform.childCount;
}
if (currentGroupIndex == 1)
{
destHealer = (destHealer + 1) % center.transform.childCount;
}
if (currentGroupIndex == 2)
{
destDpsMR = (destDpsMR + 1) % right.transform.childCount;
}
if (currentGroupIndex == 3)
{
destDpsML = (destDpsML + 1) % left.transform.childCount;
}
if (currentGroupIndex == 4)
{
destDpsRR = (destDpsRR + 1) % bottomR.transform.childCount;
}
if (currentGroupIndex == 5)
{
destDpsRL = (destDpsRL + 1) % bottomL.transform.childCount;
}
}

{
for (int i = 0; i < value; i++)
{
if (currentGroup.transform.childCount < 9)
{
Instantiate(gridCell, currentGroup.transform.position, Quaternion.Euler(90, 0, 0), currentGroup.transform);
FloatingText("+ " + value.ToString() + " CELLS");
}
}
GroupsOffset();
Positions();
}
public void CellRemove(int value)
{
for (int i = value; i > 0; i--)
{
if (currentGroup.transform.childCount > 3)
{
DestroyImmediate(currentGroup.transform.GetChild(i).gameObject);
FloatingText("- " + value.ToString() + " CELLS");
}
}
GroupsOffset();
Positions();
}

• While the images help a bit, I'm still unclear what exactly you are asking. I get that the colors are falling/ be placed in a group of 3 but thats where I'm lost already. Are the blue ones supposed to fall down? Is that red tank thing placed at the wrong place? Maybe use a different image sequence with less dots in a three step, twice. Once how it is now and a second one how it is supposed to look like. (Thats 9 images in total, can be even simple paint dots) Jun 30 at 20:43
• sorry I added a new image to better explain what the problem is. let's focus on one group only (red), forget other colors. what i need is the index to be 6 in a 3x3 grid, 3 in a 3x2 and 0 in a 3x1. if i change the index manually (from 6 to 7 for example) then it should be 7, 4 and 1. if i set it to 8 it should become 5 and 2 and so on. Jun 30 at 22:52

circleNumber = ((totalElements - 1) / 3) * 3 + index;

circleNumber = ((totalElements - 1) / 3) * 3 + (index % 3);

• i don't understand what circleNumber stands for. i though it should be the same of index. i changed my script like this destTank = ((top.transform.childCount - 1) / 3) * 3 + destTank; but it's not working. do i have to create a new var called index? Jul 1 at 13:27