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

C# List.Add Adding Last Entry x Times

Discussion in 'Scripting' started by Studio_Akiba, Oct 13, 2018.

  1. Studio_Akiba

    Studio_Akiba

    Joined:
    Mar 3, 2014
    Posts:
    1,421
    I have a bug in a code loop where only the last List.Add entry is added, but is added by the amount of times the loop is iterated.
    I believe this has something to do with where Pages page = new Pages() is instantiated, but after quite a bit of trial and error I cannot for the life of me work out what is going wrong (or, if it is that, where it needs to go).

    This code is designed to pull apart xml code, so I needed to nest loops which I think has just made it a little confusing for me to work with.

    Code (CSharp):
    1. [System.Serializable]
    2. public class PageButtons
    3. {
    4.     public string buttonKey;
    5.     public string buttonValue;
    6. }
    7.  
    8. [System.Serializable]
    9. public class Pages
    10. {
    11.     public string pageName;
    12.     public string pageContents;
    13.  
    14.     public List<PageButtons> pageButtons = new List<PageButtons>();
    15. }
    16.  
    17. void Start()
    18.     {
    19.         pages = new List<Pages>();
    20.  
    21.         //Read file into XElement
    22.         XElement rootElement = XElement.Parse(file.text);
    23.  
    24.         GetPageData(rootElement);
    25.     }
    26.  
    27. private void GetPageData(XElement element)
    28.     {
    29.         Pages page = new Pages();
    30.  
    31.         //Build XmlDocument using XElement contents
    32.         XmlDocument xDoc = new XmlDocument();
    33.         xDoc.Load(new StringReader(element.ToString()));
    34.  
    35.         //Locate <object> node
    36.         XmlNodeList nodeList = xDoc.FirstChild.SelectNodes("//object");
    37.  
    38.         //Iterate through <object> node
    39.         foreach (XmlNode node in nodeList)
    40.         {
    41.             Debug.Log(node.Attributes["name"].Value);
    42.             if(node != null)
    43.             {
    44.                 if (node.Attributes["name"].Value != "player")
    45.                 {
    46.                     string name = node.Attributes["name"].Value;
    47.                     XmlNode description = node.FirstChild;
    48.  
    49.                     page.pageName = name;
    50.                     page.pageContents = description.InnerText;
    51.  
    52.                     //If the page has buttons
    53.                     if (description.NextSibling != null)
    54.                     {
    55.                         //Locate <options> node
    56.                         XmlNodeList optList = description.NextSibling.ChildNodes;
    57.  
    58.                         foreach (XmlNode optNode in optList)
    59.                         {
    60.                             PageButtons buttons = new PageButtons();
    61.  
    62.                             XmlNode btnKey = optNode.FirstChild;
    63.                             XmlNode btnValue = btnKey.NextSibling;
    64.  
    65.                             buttons.buttonKey = btnKey.InnerText;
    66.                             buttons.buttonValue = btnValue.InnerText;
    67.  
    68.                             page.pageButtons.Add(buttons);
    69.                         }
    70.                     }
    71.                     pages.Add(page);
    72.                 }
    73.             }
    74.         }
    75.     }
    So line 71 above is where the data is added to a list, but it seems to be doing so incorrectly as it only adds "Page3" 3 times.

    Code (XML):
    1. <object name="Page1">
    2.     <description><![CDATA[This is the first page and default starting page.></description>
    3.     <options type="stringdictionary">
    4.       <item>
    5.         <key>Page2</key>
    6.         <value>This link goes to page 2</value>
    7.       </item>
    8.       <item>
    9.         <key>Page3</key>
    10.         <value>And this link goes to page 3</value>
    11.       </item>
    12.     </options>
    13.     <object name="player">
    14.       <inherit name="defaultplayer" />
    15.     </object>
    16.   </object>
    17.   <object name="Page2">
    18.     <description>This is page 2.</description>
    19.   </object>
    20.   <object name="Page3">
    21.     <description>This is page 3.</description>
    22.   </object>
    Anyone know where I am going wrong?
     
  2. roykoma

    roykoma

    Joined:
    Dec 9, 2016
    Posts:
    176
    You are editing the same page over and over again, and just adding it again and again. You are not actually adding "new" pages. You are just rewriting the first one and putting it in again.
    Adding "page" into the list and then just changing its parameters is going to edit the items inside the list too.
    Try moving
    Pages page = new Pages();
    inside the loop, so that a new page is created always.
     
    Last edited: Oct 13, 2018
  3. Studio_Akiba

    Studio_Akiba

    Joined:
    Mar 3, 2014
    Posts:
    1,421
    That's it!
    Thank you so much :D