Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Vector3 transform updates all of the instatiated objects

Discussion in 'Scripting' started by AdmiralNines, Jan 2, 2020.

  1. AdmiralNines

    AdmiralNines

    Joined:
    Mar 26, 2019
    Posts:
    6
    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.

    Code (CSharp):
    1.  
    2.     private void BuildMissionTargets()
    3.     {
    4.         ClearList(TargetPanelContent.transform);
    5.        
    6.         Debug.Log("PanelTransform: " + TargetPanelContent.transform.position.ToString());
    7.         // define where to place the next list item
    8.         Vector3 nextItemPosition = TargetPanelContent.transform.position;
    9.         RectTransform rt = TargetPanelContent.GetComponent<RectTransform>();
    10.         int totalHeight = height; // the size of the panel.
    11.         nextItemPosition -= listItemHeight;
    12.         Debug.Log("NextItemPosition: " + nextItemPosition.ToString());
    13.  
    14.         // Build a new gameobject and throw it in the targetpanel, for the first heading.
    15.         GameObject listHeading = Instantiate(listItem, nextItemPosition, Quaternion.identity, TargetPanelContent.transform);
    16.         GameObject name = listHeading.transform.GetChild(0).gameObject;
    17.         name.GetComponent<Text>().text = Colony.ColonyName;
    18.         totalHeight += height;
    19.         nextItemPosition -= listItemHeight;
    20.         Debug.Log("NextItemPosition: " + nextItemPosition.ToString());
    21.  
    22.         // build the second heading.
    23.         GameObject structH2 = Instantiate(listItem, nextItemPosition, Quaternion.identity, TargetPanelContent.transform);
    24.         structH2.GetComponent<Button>().interactable = false;
    25.         name = structH2.transform.GetChild(0).gameObject;
    26.         name.GetComponent<Text>().text = "Structures";
    27.         totalHeight += height;
    28.         nextItemPosition -= listItemHeight;
    29.         Debug.Log("NextItemPosition: " + nextItemPosition.ToString());
    30.  
    31.         // Go through each of the structures and place them as an option.
    32.         foreach (Structure s in Colony.BuiltStructures)
    33.         {
    34.             GameObject li = Instantiate(listItem, nextItemPosition, Quaternion.identity, TargetPanelContent.transform);
    35.             name = li.transform.GetChild(0).gameObject;
    36.             name.GetComponent<Text>().text = s.structureName;
    37.  
    38.             nextItemPosition -= listItemHeight;
    39.             totalHeight += height;
    40.         }
    41.  
    42.         Debug.Log("NextItemPosition: " + nextItemPosition.ToString());
    43.  
    44.         // build the second heading.
    45.         GameObject armiesH2 = Instantiate(listItem, nextItemPosition, Quaternion.identity, TargetPanelContent.transform);
    46.         armiesH2.GetComponent<Button>().interactable = false;
    47.         name = armiesH2.transform.GetChild(0).gameObject;
    48.         name.GetComponent<Text>().text = "Armies";
    49.         totalHeight += height;
    50.         nextItemPosition -= listItemHeight;
    51.  
    52.         foreach (Army a in Colony.ResidentArmies)
    53.         {
    54.             GameObject li = Instantiate(listItem, nextItemPosition, Quaternion.identity, TargetPanelContent.transform);
    55.              name = li.transform.GetChild(0).gameObject;
    56.             name.GetComponent<Text>().text = a.ArmyName;
    57.  
    58.             nextItemPosition -= listItemHeight;
    59.             totalHeight += height;
    60.         }
    61.  
    62.         // build the second heading.
    63.         GameObject agentsh2 = Instantiate(listItem, nextItemPosition, Quaternion.identity, TargetPanelContent.transform);
    64.         agentsh2.GetComponent<Button>().interactable = false;
    65.         name = agentsh2.transform.GetChild(0).gameObject;
    66.         name.GetComponent<Text>().text = "Agents";
    67.         totalHeight += height;
    68.         nextItemPosition -= listItemHeight;
    69.  
    70.         foreach (Agent a in Colony.HousedAgents)
    71.         {
    72.             GameObject li = Instantiate(listItem, nextItemPosition, Quaternion.identity, TargetPanelContent.transform);
    73.              name = li.transform.GetChild(0).gameObject;
    74.             name.GetComponent<Text>().text = a.AgentName;
    75.  
    76.             nextItemPosition -= listItemHeight;
    77.             totalHeight += height;
    78.         }
    79.  
    80.         Debug.Log("NextItemPosition: " + nextItemPosition.ToString());
    81.  
    82.         Debug.Log("PanelTransform: " + TargetPanelContent.transform.position.ToString());
    83.         rt.sizeDelta = new Vector2(0, totalHeight + height); // resize that target panel.
    84.  
    85.         Debug.Log("PanelTransform: " + TargetPanelContent.transform.position.ToString());
    86.     }
    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?
     
  2. adi7b9

    adi7b9

    Joined:
    Feb 22, 2015
    Posts:
    181
    Debug.Log("NextItemPosition: " + nextItemPosition.ToString()); has different values?

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

    AdmiralNines

    Joined:
    Mar 26, 2019
    Posts:
    6
    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
     
  4. AdmiralNines

    AdmiralNines

    Joined:
    Mar 26, 2019
    Posts:
    6
    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.
     
  5. AdmiralNines

    AdmiralNines

    Joined:
    Mar 26, 2019
    Posts:
    6
    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 :(
     
  6. adi7b9

    adi7b9

    Joined:
    Feb 22, 2015
    Posts:
    181