Instantiating a serializable class and variable

Here is what I’m doing (Shorter version :D)

  • I’ve created an empty serializable class with only a public integer variable “myNumber” inside it.
  • I then create an instance “A” of the serializable class. copy “A” to a new variable “B”.
  • in the editor set the variable “myNumber” inside “A” and see what happen to “myNumber” in “B”.
  • in the editor set the variable “myNumber” inside “B” and see what happen to “myNumber” in “A”.

##Well, what happen is that when you change “B” then “A” changes as well, so they seem to point to the same variable.
##But when you change “A” nothing happens, not even “A” changes.

What’s going on with the Serializable class ?

Why only B can be changed ?

Any explanation would be really welcome !

So here are the scripts :


// caller script attached to a game object and used to instantiate my serializable classe
using UnityEngine;

public class TestClassCaller : MonoBehaviour {
	public TestClassSerializable serializableA, serializableB;

	void Start () {
		serializableA = new TestClassSerializable();
		serializableB = serializableA;
	}
}

// Serializable class
[System.Serializable]
public class TestClassSerializable {
	public int myNumber;
}

This is what documentation says about that:

The Inspector window

When you view or
change the value of a GameObject ’s
component field in the Inspector
window, Unity serializes this data and
then displays it in the Inspector
window. The Inspector window does not
communicate with the Unity Scripting
API when it displays the values of a
field.

If you use properties in your script,
any of the property getters and
setters are never called when you view
or change values in the Inspector
windows as Unity serializes the
Inspector window fields directly. This
means that: While the values of a
field in the Inspector window
represent script properties, changes
to values in the Inspector window do
not call any property getters and
setters in your script

from here

But that does not explain the normal behaviour of changing properties in inspector while running the game and changes actually affect the game.