Changeing a classes properties in the inspector does not preserve references?

Hi, if I run the following code pressing any of the buttons increases the integer in the class (there is only one class created in this example) and so the value changes in all it’s references, as I’d expect.

If I then change the value in the inspector for aTestClass, or in either of the Lists, this no longer works and pressing a button will only increase the integer in one location. I do not understand how this is possible are they no longer all referencing the same class?

#pragma strict
import System.Collections.Generic;
var aTestClass: ATestClass = new ATestClass();
var classes1: List.<ATestClass> = new List.<ATestClass>();
var classes2: List.<ATestClass> = new List.<ATestClass>();
class ATestClass{
	var anInt: int = 0;
	function ATestClass(){
	}
}
function Start(){
	classes1.Add(aTestClass);
	classes2.AddRange(classes1);
}
function OnGUI(){
	if(GUILayout.Button("Loose Class")){
			aTestClass.anInt++;
	}
	if(GUILayout.Button("Classes1 Inc")){
			classes1[0].anInt++;
	}
	if(GUILayout.Button("Classes2 Inc")){
			classes2[0].anInt++;
	}
	GUILayout.Label("aTestClass: " + aTestClass.anInt.ToString());
	GUILayout.Label("classes1[0]: " + classes1[0].anInt.ToString());
	GUILayout.Label("classes2[0]: " + classes2[0].anInt.ToString());
}

Each instance of a GameObject with this attached to it will get their own instances of the variables, you clearly create new instances, line 3-5. I’m not sure if you are passing in any kind of reference to another object, but i don’t see it in the code above.

No. This code lives on a single object and only one class is created. Line 3 is creating a new class, Line 4 and 5 make a pair of empty lists, then in line 12 and 13 the original class from line 3 is added to both of these lists. If you test the script you will notice that if you don’t fiddle with the inspector pressing any of the buttons changes the value in all of the visible references to the class, but after changing any of them this is no longer the case.

…bump?