- Home /
Why does the prefabOverride not work on fields marked as SerializeField?
Hi there,
I have the following problem: I want to run a custom serialization algorithm on a component (MonoBehaviour) which uses a lot of private fields being set by properties. The fields are set by an custom editor by using the properties. The fields are using the SerializeField attribute.
E.g.:
/// <summary>
/// The custom animation name.
/// </summary>
[SerializeField]
private string animationName;
/// <summary>
/// The custom animation name.
/// </summary>
public string AnimationName
{
get
{
return this.animationName;
}
set
{
if (this.animationName == value)
{
return;
}
this.animationName = value;
this.RunValidationAlgorithmOnTheNewName();
}
}
The component using this field is part of a gameobject been instantiated from a prefab. The value gets changed in the prefab instance but not applied to the prefab, which is the way its intented.
When saving and loading this gameobject to and from a scene everything works fine. But when using the custom serialization it simply does not work for SerializeField-fields. This is the way I try to determine if a field has been changed from its original prefab value:
// use untiy to get information about modified values
SerializedObject serObj = new SerializedObject(theScriptComponent);
SerializedProperty property = serObj.GetIterator();
property.Next(true);
// store attributenames of modified values
do
{
// This is where Unity says false on SerializeField fields.
if (property.prefabOverride)
{
Debug.Log("Property '" + property.name + "' has an overwritten value.");
}
}
while (property.Next(false));
Is there a way to get the correct modification value of the prefab instances fields, which are marked with SerializeField? Eventually it works when using the scene format.
Thanks in advance.
Your answer
Follow this Question
Related Questions
How to update serializedField showed on the inspector by script? 1 Answer
Change scale in lightmap (Prefab property) by script 0 Answers
Is using "OnValidate" the proper way to set references to serialized fields on prefabs? 0 Answers
How to access property of a prefab before Instantiating. 1 Answer
Apply only a single value to prefab 1 Answer