Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Question Custom class array, populates all indexes with same data.

Discussion in 'Scripting' started by Nachman, Sep 7, 2023.

  1. Nachman

    Nachman

    Joined:
    Oct 22, 2013
    Posts:
    26
    Hi and thanks in advance, I am going nuts trying to fix this issue.

    So I have a server call from Unity that returns several database rows together, those get split into separate rows and further into separate fields. I also have a custom class type that takes all the fields and then gets stored in an array containing the custom class objects(Each with its fields). In principle works fine but when I try to access the array every index returns the same object always being the last one to populate the array. I don't get data Type errors, the class contains different data types and they get stored well in the array. It also has the right size for the array, it just populates it with the same object every index.

    Any ideas? Thanks again.

    Code (CSharp):
    1.  
    2.  
    3. public class SearchResult : MonoBehaviour
    4. {
    5.     public int postID;
    6.     public string title;
    7.     public float longitude;
    8.     public float latitude;
    9.     public float altitude;
    10.     public string timeStamp;
    11. }
    12.  
    13.  
    14. public class SearchManager : MonoBehaviour
    15. {
    16.  public SearchResult ObjClss = new SearchResult();
    17.  public List<SearchResult> resultData = new List<SearchResult>();
    18.  public string[] rowInfo;
    19.  
    20.  public IEnumerator GetGeoLocationFromServer()
    21.  {
    22.        
    23.         WWWForm form = new WWWForm();
    24.        
    25.         using (UnityWebRequest www = UnityWebRequest.Post("https://xxxxxxx/xxx.php", form))
    26.         {
    27.  
    28.             yield return www.SendWebRequest();
    29.             string rawresponse = www.downloadHandler.text;
    30.             //Splits rows by post so becomes a string array of posts and ends "*0
    31.              string[] postRows = rawresponse.Split('*');
    32.  
    33.                
    34.             //Note that the reason why there is an offset of -2 is because the loop returns one extra useless value and we want to account for 0 as an index.
    35.             for (int i = 0; i <= postRows.Length - 2; i++)
    36.             {
    37.                
    38.                 //Next we create a string array for each post field
    39.  
    40.                 rowInfo = postRows[i].Split(',');
    41.  
    42.                 populateData(rowInfo[0], rowInfo[1], rowInfo[2], rowInfo[3], rowInfo[4], rowInfo[5], i);
    43.                 Array.Clear(rowInfo,0,6);
    44.                }
    45.              ConvertToStringArray()
    46.          }
    47.  
    48.  private void populateData(string A, string B, string C, string D, string E , string F, int n)
    49.     {
    50.        
    51.         ObjClss.latitude = float.Parse(A);
    52.         ObjClss.longitude = float.Parse(B);
    53.         ObjClss.altitude = float.Parse(C);
    54.         ObjClss.title = D;
    55.         ObjClss.timeStamp = E;
    56.         ObjClss.postID = int.Parse(F);
    57.  
    58.         Debug.Log(n);
    59.         resultData.Insert(n, ObjClss);
    60.         Debug.Log(resultData[n].postID);
    61.  
    62.     }
    63.  
    64.  public void ConvertToStringArray()
    65.     {
    66.       foreach (SearchResult obj in resultData)
    67.         {
    68.           Debug.Log(resultData[1].postID);
    69.         }
    70.  
    71.      }
    72. }
    73.  
    74.  

    So the Debug.Logs on lines 58 and 60 show the right data, but when accessed like on line 68 all indexes show the same result, the right amount of results but just the same result in each index of the array, with no problem with data types.

    Thanks again!
     
    Bunny83 likes this.
  2. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    3,541
    You said "objects", plural. However you only ever create one single instance here:
    So of course you only have one class instance. You just change the data of that one instance and add references to that same instance to your list. So all instance references reference the same single object. You have to actually create a new class instance inside "populateData".

    edit:
    ps: Also your SearchResult class is derived from MonoBehaviour which makes no sense. You can not / should not create MonoBehaviour derived classes with new. In your case, do not derive from MonoBehaviour. This is a pure data class and not a component. Just create a plain class with the System.Serializable attribute on top.
     
  3. Nachman

    Nachman

    Joined:
    Oct 22, 2013
    Posts:
    26
    Dam! You are right! I just came back to it after a long break and I feel part of my brain still on the beach.

    Thanks a lot!
     
    Bunny83 likes this.