List changes affecting Array

So currently I have an Array and a List. Items from the Array are added to my List. The problem I’m having is when I subtract from the List it affects the original number in the Array.

Example: myList.number -= 1;

Which causes myArray[0].number to also minus 1.

Do I need to make some variable private? Or I am not sure.

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.

This is likely because the items stored in your array and list are both reference type variables. The difference between reference types and value types is an important one, and every programmer would be well advised to study this concept well.

In very short summary, and specific to your situation:

If you have reference types in both the List and the Array, then the references will point to the same actual object, i.e. you’re not storing two copies of the object, you’re storing two references to them. Consider the following example:

You have a house. The house has 4 doors.

You take the street address to your house and store it in the List. Then you take the same address and store it in the Array.

Now, go to the address stored in the List. Remove a door from the house at that address. It now has 3 doors.

Does the house at the address stored in the Array still have 4 doors?

No, 'cause it’s the same house. :wink: