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?