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());
}