- Home /
Prevent Property from Serializing In Prefab but not on Instances
Trying to figure out if there's a way to prevent prefabs from receiving "apply" on certain variables in a script? I want it to have a property with a default value, but when I edit an instance in the scene and click "apply" the field stays bold and doesn't save to the prefab? Possible?
I have a workaround, but i think it's not the right thing to do, I'll post it as an answer. Edit: $$anonymous$$y workaround didn't work, sorry. But you can have a gameobject that hold this data to all your objects and them access it.
Bummer, but thanks for checking! I was hoping for some sort of attribute that would allow the property to show in the editor but not serialize for anything but instances.
$$anonymous$$aybe Nested Prefabs can help you, a prefab with two other prefabs inside, one is the instance variables, which you never apply and the other is the shared variables which you can apply. Anyway it's confusing haha.
Answer by vkidd · Sep 18, 2013 at 11:57 PM
I've been trying to figure this out as well. It would be great to have a flag like [System.NonSerialize] to make a public variable non-prefab serializable. I come across this use case very often; where many prefab instances have 99% of the same setup, but need some scene specific tweaking. Making those changes is risky because someone hitting apply prefab will wipe them on all instances.
Answer by gjh333 · Nov 15, 2018 at 02:41 PM
Came across this problem recently. Try this:
using UnityEditor
[ExecuteInEditMode]
public class DontCommitPropertyA : MonoBehaviour, ISerializationCallbackReceiver
{
public string PropertyA;
public string PropertyB;
OnBeforeSerialize()
{
if (PrefabUtility.GetPrefabType(gameObject) == PrefabType.Prefab)
{
// Here you can reset values if the object is a source prefab
PropertyA = "";
}
}
OnAfterDeserialize()
{
}
}
You may want to wrap some of this code with ifdefs for runtime. This works for me :)
Your answer
Follow this Question
Related Questions
When does the "InstanceID" get assigned, and does it seralize with the Object? 2 Answers
Affect All Instances Of A Prefab 3 Answers
How to remove a specific instance of a prefab from List? 1 Answer
Gameobject missing camera when instance prefab 3 Answers
How can I set the material on an instance after creating it? 1 Answer