- [Instructor] When working with code,
you'll inevitably need to work
with many types of variables.
Ultimately, all variables have what is called a data type.
The two primary data types are
value types and reference types.
Variables like integers, floats, doubles, booleans,
and characters are all value types.
Additionally, there are complex variable types,
known as Structs.
Structs are value data types that simply contain
one or more other variables.
The two most common Structs in Unity
are Vector3 and Quaternion.
The list of reference types is much simpler.
Basically, any variable that is an object
of a class is a reference type.
The two most common classes are thus the two most common
reference types in Unity, Transform and GameObject.
You might be wondering what the difference is
between value and reference types.
Simply put, value type variables actually contain
some value, all reference type variables contain
a memory address where the value is stored.
The result is that if a value type changes,
only that specific variable is affected.
If a reference type changes, however,
all variables that contain that memory address
will also be affected.
As an analogy, consider this.
Charles has a nice blue house, full of his favorite things.
Quentin sees Charles' house online and is jealous,
and so makes an exact copy and fills it with duplicates
of everything that Charles owns.
Creepy, right?
This is like assignment for value types.
You make a copy of the variable.
Quentin doesn't own Charles' house,
and nothing he changes in his house will affect the original
not even painting it red.
If Quentin had Charles' address,
he wouldn't need to copy the house.
He could simply visit anytime he likes
and see its contents instead.
This is like assignment for reference types.
You make a note of the memory address of the value you need
and return to that address when you need
to know the value of the variable.
Let's take a look at this in action.
We have here a scene with our friend Sam-a-flang in it.
Sam-a-flang has a script named
Datatype Script attached to it.
Inside the Datatype Script,
we first create a Vector3 variable named currentPosition
and assign to it the value of the transform's position.
We then set currentPosition equal to a new Vector3
with a value of zero, two, zero.
Since we know that currentPosition is a Vector3 struct,
and that structs are value types,
we know that only currentPosition
is affected by this line of code.
The transform's position is unaffected.
If we go back into Unity, we can run our scene
and confirm that the Sam-a-Flang does not move.
Value types contain their own copy of data
and changing them only affects that specific variable.
Back in the Datatype Script, let's try another approach.
Let's start by creating a Transform variable named tran
and setting it equal to the object's transform.
Then, let's set tran's position to a new Vector3
with value of zero, two, zero.
Since Transform is a class,
we know that tran is a reference type.
Furthermore, since we assigned the object's transform
to the variable tran using the assignment operator,
we know that they point at the same memory address.
The result is that changing one changes the other.
Back in Unity, we can run our scene.
We should notice immediately
that the Sam-a-Flang has moved upwards,
even though we never directly changed
the value of the Sam-a-Flang's transform.
We were able to indirectly change it,
by using a reference variable.