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: