You have to read something about Reference Types as opposed to Value Types. I will try to give you a quick hint.
In C# and most languages, the types can be either reference types or value types.
The main difference is where the values of the objects are ‘stored’.
In a value type, for example int or float, the value itself is stored in the variable. In a reference type, the objects are stored somewhere in memory, and variables just hold a reference to the object, a pointer, an address.
For example, you have something like this:
struct myValueTypeFriend{
string Name;
string Phone;
}
myValueTypeFriend a = new myValueTypeFriend{Name = "David", Phone="12345"};
myValueTypeFriend b = a;
a.Phone = "555666";
// b.Phone still is "12345";
That is because the values are stored in the variables, and when you use = to assign the value of one variable to another, what your are doing is making a copy of the value. Essentially cloning it. So that is why after copying the friend from a to b, if a modifies one of the fields or properties, that doesn’t affect the clone that b got before.
However check this example now:
class myReferenceTypeFriend{
string Name;
string Phone;
}
myReferenceTypeFriend a = new myReferenceTypeFriend{Name = "David", Phone="12345"};
myReferenceTypeFriend b = a;
a.Phone = "555666";
// b.Phone is now "555666";
That happens because a and b are now reference type variables. If you assign the value of one reference type variable to another, what you are doing is making another variable point to the same object the first variable is pointing, so any change to the object will be visible for both variables.
As you see, if you create a class, objects of that class will be reference type objects. If you create a struct, objects of that struct will be value type objects.
What is happening in your example is that you have a List<Debuff> myList
and a Debuff[] myArray
.
I bet you have defined Debuff
as a class
that has, among other things, a member called number
of float
type. Right?
When you add a Debuff
object to the list, you are just adding a pointer to the real Debuff
object, that is somewhere in memory. When you copy that item from the list to the array (or viceversa), you are just copying the pointer, the reference. So when you access myList[0].number
and myArray[0].number
, you are accessing the same object and the same field (if indeed both first elements are the same).
You have two alternatives.
First:
When you add the Debuff
from the array to the list, you must make a clone of it. For that, you could add a Clone()
method to the Debuff
class that creates a new Debuff object and copies the values of every field from the original to the new one. I recommend you checking this 1.
Second:
Change Debuff
so that it’s a struct instead of a class. However beware, structs cannot inherit from other structs or classes (only interfaces). If your Debuff
is making heavy use of inheritance you will have to use the other alternative. Also, if your Debuff contains members (fields) that are of reference type, you will encounter the same problem. Read the link above for more information.
In C# use
struct Debuff
{
//code
}
In UnityScript, use
class Debuff extends System.ValueType
{
//code
}
if it doesn’t support the struct
keyword.