How to 'link' variables?

Hi,

I would like to know something that I need for a script in my game.
Let’s say I have a Class:

public class ExampleClass{
var string1 : String;
var int1 : int;
var boolean1 : boolean;

}

And now if I would setup two different objects, one with a Script called ClassHolder which holds just one variable of the above described ExampleClass that is called exampleClassOnClassHolder. And one that holds this script:

var currentlyConnectedClass : ExampleClass;

function AssignConnectedClass( obj : Transform){
currentlyConnectedClass = obj.GetComponent(ClassHolder).exampleClassOnClassHolder;
}


function AdjustSomeVariables(){
currentlyConnectedClass.string1 = "Test";
currentlyConnectedClass.int1++;
currentlyConnectedClass.boolean1 != currentlyConnectedClass.boolean1;
}

If I would call the first function, would that mean that currentlyConnectedClass will just copy the other object’s ExampleClass variable, or completely link it?
What I mean with ‘link’ is that if I would call AdjustSomeVariables(); the variable on this script would change as would the one on the ClassHolder script. I believe this is not the case, so how could I ‘link’ variables as described as above? Or a method to achieve the same result:)

I know this is a really vague question but I tried to explain it as best as I could!
Thank you in advance!

If i understand your question right, the short answer is: it does get “linked”.

You should read up on “reference types” and “value types”. It’s a very basic concept in object oriented programming and without understanding it you’ll run into problems all the time.

In short, variable types like int, float, char (and all Structs: Vector2, Vector3) etc. are value types. Any time you assign a value to a value type variable or pass one as a parameter to a function, it gets “copied” => changing the original variable doesn’t change the newly assigned one.

Reference type variables point to an object in memory. When you assign or pass them around, you still have just one object in the memory, but multiple variables referencing it. This is the case in your example.

// VALUE TYPES
var a:int = 1;
var b:int = a; // int is value type so the value of a gets copied into b
a = 2; // b is still 1 after this. a and b are not "linked" (they are not pointing to the same object in memory)

var a:float = 1;
changeFloat(a)
...
void changeFloat(parameter:float)
{
    parameter = 2; // <- value type
    // the variable a outside of the function is still 1
    // because float is a value type, "parameter" and "a" are not "linked" 
}

// REFERENCE TYPES
var a:GameObject = new GameObject("carrot");
var b:GameObject = new GameObject("apple");
a = b;
// now both variables a and b are pointing to the same object (b / "apple")
// we have lost "carrot" because no variable is pointing to it

a.name = "peanut";
Debug.Log(a.name);
Debug.Log(b.name);
// prints out "peanut" twice, since both variables are just references to the 2nd created GameObject 

var a:GameObject = new GameObject("horse");
a.transform.position = Vector3.zero;

void moveHorse(parameter:GameObject)
{
    parameter.transform.position = Vector3.one;
    // the original "horse" we created outside the function is moving
    // because "parameter" and "a" are just references to the same object
}

Anything you declare a class (like ExampleClass) is a reference type, so you are passing around a reference to it. You have just one of that object in the memory and when you do AdjustSomeValues(), you adjust the values of that specific object no matter shich variable you use to do so.