- Home /
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;
}
Class objectA = new Class();
Class objectB = objectA;
This means A and B are pretty much the same thing, two references to the same address in memory. What you need is a deep copy.
[System.Serializable]
public class TestClassSerializable {
public int myInt;
public string myString;
public TestClassSerializable DeepCopy(){
TestClassSerializable temp = new TestClassSerializable ();
temp.myInt = this.myInt;
temp.myString = this.myString;
return temp;
}
}
This will create a new object, totally independant from the the first but with the values of the first. Now when you change one, the other remains.
Note that if your object contains an object reference as member then you need to do the same process of deep copy or your two different objects will have a common reference.
I would think the ScriptableObject class has an override of the equal sign and this is why you do not see this problem.
This means A and B are pretty much the same thing, two references to the same address in memory. What you need is a deep copy.
That's the problem right here, you'd expect that if I do A = B = C they would all point to the same thing or be passed by value ... but they are not, only B can be changed and values will be the same in A and $$anonymous$$ain. But if you try to change A or $$anonymous$$ain, no value is set, not even on A or $$anonymous$$ain. It behave like B is overwriting A and $$anonymous$$ain.
I just want to be able to change A or B or $$anonymous$$ain and be able to see the same changes in other instances.
Answer by Baalhug · Nov 30, 2018 at 07:59 PM
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.
Your answer
Follow this Question
Related Questions
Error serializing a class to save / load 2 Answers
Instantiated objects into serialized property? 2 Answers
Distribute terrain in zones 3 Answers
How can I get functionality similar to OnValidate for a custom class? 4 Answers
Multiple Cars not working 1 Answer