Search Unity

strange issue with lists

Discussion in 'Scripting' started by twangsta, May 12, 2013.

  1. twangsta

    twangsta

    Joined:
    Mar 28, 2013
    Posts:
    17
    Hi,

    I've got a strange issue with handling hashtables of lists.

    App.vertexes is a hashtable of lists of longs.
    When I derefrence say App.vertexes[vert_B_index... ] and make changes to the list,
    it also changes values in App.vertexes of other keys in that hash table.

    Any help would be greatly appreciated, thanks in advance.

    Sid.


    Code (csharp):
    1.  
    2.             // update vert_B's segments
    3.             Debug.Log ("vert_A_index: " +  vert_A_index);
    4.             Debug.Log ("vert_B_index: " +  vert_B_index);
    5.             List<long> vert_B_segments = (List<long>) App.vertexes[vert_B_index.ToString ()];
    6.             //vert_B_segments.Remove(seg_A_index);
    7.             //vert_B_segments.Add(seg_B_index);
    8.             //App.vertexes[ vert_B_index.ToString()] = vert_B_segments;
    9.             List<long> vert_B_segments_test = (List<long>) App.vertexes[vert_B_index.ToString ()];
    10.             foreach ( long index in vert_B_segments_test )
    11.                 Debug.Log ("vert_B_segments: " + index);
    12.            
    13.             List<long> vert_A_segments_test = (List<long>) App.vertexes[vert_A_index.ToString()];          
    14.             foreach ( long index in vert_A_segments_test) {
    15.                 Debug.Log ("vert_A_segments: " + index);
    16.             }  
    17.  
     
    Last edited: May 12, 2013
  2. twangsta

    twangsta

    Joined:
    Mar 28, 2013
    Posts:
    17
    Solved.... turns out I was initialising the same list object into two keys in the hash table.

    Live and learn... putting the same object into static storage in more than one place will not create a clone.. but a reference to one single copy!

    Phew ... now I can move on .. yey!