Vector3 transform updates all of the instatiated objects

Alright, So I’m very new to C#. Please forgive me. I’m sure this is a really easy fix, but It’s been a while since I’ve worked on this project and I think I found the bug but I’m not sure how to fix it:

I create a menu, and that menu lays out a bunch of buttons that should come one after the other. Instead, all of the buttons end up on top of each other in the exact same spot at the bottom of the viewport.

DISCLAIMER: I know this code could be much cleaner and DRY but i’m working out the kinks as I go.

    private void BuildMissionTargets()
    {
        ClearList(TargetPanelContent.transform);
       
        Debug.Log("PanelTransform: " + TargetPanelContent.transform.position.ToString());
        // define where to place the next list item
        Vector3 nextItemPosition = TargetPanelContent.transform.position;
        RectTransform rt = TargetPanelContent.GetComponent<RectTransform>();
        int totalHeight = height; // the size of the panel.
        nextItemPosition -= listItemHeight;
        Debug.Log("NextItemPosition: " + nextItemPosition.ToString());

        // Build a new gameobject and throw it in the targetpanel, for the first heading.
        GameObject listHeading = Instantiate(listItem, nextItemPosition, Quaternion.identity, TargetPanelContent.transform);
        GameObject name = listHeading.transform.GetChild(0).gameObject;
        name.GetComponent<Text>().text = Colony.ColonyName;
        totalHeight += height;
        nextItemPosition -= listItemHeight;
        Debug.Log("NextItemPosition: " + nextItemPosition.ToString());

        // build the second heading.
        GameObject structH2 = Instantiate(listItem, nextItemPosition, Quaternion.identity, TargetPanelContent.transform);
        structH2.GetComponent<Button>().interactable = false;
        name = structH2.transform.GetChild(0).gameObject;
        name.GetComponent<Text>().text = "Structures";
        totalHeight += height;
        nextItemPosition -= listItemHeight;
        Debug.Log("NextItemPosition: " + nextItemPosition.ToString());

        // Go through each of the structures and place them as an option.
        foreach (Structure s in Colony.BuiltStructures)
        {
            GameObject li = Instantiate(listItem, nextItemPosition, Quaternion.identity, TargetPanelContent.transform);
            name = li.transform.GetChild(0).gameObject;
            name.GetComponent<Text>().text = s.structureName;

            nextItemPosition -= listItemHeight;
            totalHeight += height;
        }

        Debug.Log("NextItemPosition: " + nextItemPosition.ToString());

        // build the second heading.
        GameObject armiesH2 = Instantiate(listItem, nextItemPosition, Quaternion.identity, TargetPanelContent.transform);
        armiesH2.GetComponent<Button>().interactable = false;
        name = armiesH2.transform.GetChild(0).gameObject;
        name.GetComponent<Text>().text = "Armies";
        totalHeight += height;
        nextItemPosition -= listItemHeight;

        foreach (Army a in Colony.ResidentArmies)
        {
            GameObject li = Instantiate(listItem, nextItemPosition, Quaternion.identity, TargetPanelContent.transform);
             name = li.transform.GetChild(0).gameObject;
            name.GetComponent<Text>().text = a.ArmyName;

            nextItemPosition -= listItemHeight;
            totalHeight += height;
        }

        // build the second heading.
        GameObject agentsh2 = Instantiate(listItem, nextItemPosition, Quaternion.identity, TargetPanelContent.transform);
        agentsh2.GetComponent<Button>().interactable = false;
        name = agentsh2.transform.GetChild(0).gameObject;
        name.GetComponent<Text>().text = "Agents";
        totalHeight += height;
        nextItemPosition -= listItemHeight;

        foreach (Agent a in Colony.HousedAgents)
        {
            GameObject li = Instantiate(listItem, nextItemPosition, Quaternion.identity, TargetPanelContent.transform);
             name = li.transform.GetChild(0).gameObject;
            name.GetComponent<Text>().text = a.AgentName;

            nextItemPosition -= listItemHeight;
            totalHeight += height;
        }

        Debug.Log("NextItemPosition: " + nextItemPosition.ToString());

        Debug.Log("PanelTransform: " + TargetPanelContent.transform.position.ToString());
        rt.sizeDelta = new Vector2(0, totalHeight + height); // resize that target panel.

        Debug.Log("PanelTransform: " + TargetPanelContent.transform.position.ToString());
    }

So what I think is happening is that the nextItemPosition variable is referenced in each of the instatiations for the buttons, and therefore everytime it updates it it updates all of the button positions. How do I fix that?

Debug.Log("NextItemPosition: " + nextItemPosition.ToString()); has different values?

Put manually 2-3 buttons and see how it works.

Yeah, the debug.log shows a different value everytime it posts. What’s wierd, is it ends up at -540 in the end, and yet all the buttons end up at -273

if I do
GameObject listHeading = Instantiate(listItem, TargetPanelContent.transform, false);
instead of
GameObject listHeading = Instantiate(listItem, nextItemPosition, Quaternion.identity, TargetPanelContent.transform);

then it shows up in the center of the window instead of the bottom.

alright, i made some advancement.

I switched them all to Instantiate(listItem, TargetPanelContent.transform, false) and let them just float above each other in the center of the window. Then I added a “Vertical Layout Group” component to their parent. That worked nicely. However, I did have to check “Control Child Size” for some reason in order to get them to show up.

I wish I understood better why :frowning:

https://docs.unity3d.com/Manual/script-VerticalLayoutGroup.html

Control Child Size - Whether the Layout Group controls the width and height of its child layout elements