Problem with "Add" method for List

Hi

I have made the following test script to highlight a problem I have with adding elements to a list - it is a list of object descriptions, where object descriptions is defined as a class.
My test script looks like this:

#pragma strict

class MyObjectDescription {
   var objectName : String;
   var isCombinedMesh : boolean;
   var objectInPlace : boolean;
}
static var myObjectList = new List.<MyObjectDescription>();
static var myCurrentObject = new MyObjectDescription();

function Start () {

    // Assign the name Joe to myCurrentObject
    myCurrentObject.objectName = "Joe";

    // Put this myCurrentObject in myObjectList - the first item added
    myObjectList.Add(myCurrentObject);
    print ("List index 0: " + myObjectList[0].objectName);


    // Assign the name Bob to myCurrentObject
    myCurrentObject.objectName = "Bob";

    // Put this myCurrentObject in myObjectList - the second item added
    myObjectList.Add(myCurrentObject);
    print ("List index 0: " + myObjectList[0].objectName);
    print ("List index 1: " + myObjectList[1].objectName);


    // Assign the name Laura to myCurrentObject
    myCurrentObject.objectName = "Laura";

    // Put this myCurrentObject in myObjectList - the third item added
    myObjectList.Add(myCurrentObject);
    print ("List index 0: " + myObjectList[0].objectName);
    print ("List index 1: " + myObjectList[1].objectName);
    print ("List index 2: " + myObjectList[2].objectName);
}

When I run this script the Add method doesn’t just add a new member to the list, it assigns the same content to all existing members in the list as well.

What am I doing wrong? I wanted to have the list become “Joe”, “Bob”, “Laura”.

But the print out looks like this:

1732-printout.png

Your problem is not the list but the “myCurrentObject” variable. Classes are passed by reference, that means instead of copying the content of a memory location, only the address of that location is copied into the list. So when you do something like

myObjectList.Add(myCurrentObject);

both, myCurrentObject AND the object in the list will both point to the exact same object in memory. When you now change myCurrentObject.objectName, that name will change for myCurrentObject and the object in the list.

To prevent that behaviour, you can create a new object (and therefore reserve a new memory location). So you code should look more like

myCurrentObject.objectName = "Joe";

// Add a copy of the memory address that myCurrentObject points to to the list
myObjectList.Add(myCurrentObject);

// create a new object in memory
myCurrentObject = new MyObjectDescription();
// Assign the name Bob to myCurrentObject
myCurrentObject.objectName = "Bob";

// Add a copy of the new memory address that myCurrentObject points to to the list
myObjectList.Add(myCurrentObject);

this is Wayyyyyyyyyyyyyyyyy old but for those who are new here seeing this. You could also do

myObjectList.add(myCurrentObject = new MyObjectDescription);

something shorter than the answer above.