D
Daniel
Hey guys
I have an instance of an object say:
List<Object> myList = new List<Object>();
Object myObject = new Object();
myObject.PositionVector = new Vector3(10,10,10);
myList.Add(myObject);
With the above code as an accurate example i do that in a loop. And on each
iteration i change the position vector, however when i do this and check my
list it has correctly added each new instance as a separate entry but they
all have the position of the last one to be added. When i steped through it
in debug this was verified, the correct positon details are in on one
iteration and on the next iteration immediately after the new instance has
its position changed so too does the one already in my List.
I presume that a new memory allocation isn't being made and so on setting
position it overwrites the same memory my list is looking at but why? I
thought the 'new' operator explicitly tells it to create a new memory
allocation? Am i missing something? Anyone know how i can prevent this
situation.
Thanks
I have an instance of an object say:
List<Object> myList = new List<Object>();
Object myObject = new Object();
myObject.PositionVector = new Vector3(10,10,10);
myList.Add(myObject);
With the above code as an accurate example i do that in a loop. And on each
iteration i change the position vector, however when i do this and check my
list it has correctly added each new instance as a separate entry but they
all have the position of the last one to be added. When i steped through it
in debug this was verified, the correct positon details are in on one
iteration and on the next iteration immediately after the new instance has
its position changed so too does the one already in my List.
I presume that a new memory allocation isn't being made and so on setting
position it overwrites the same memory my list is looking at but why? I
thought the 'new' operator explicitly tells it to create a new memory
allocation? Am i missing something? Anyone know how i can prevent this
situation.
Thanks