Search Unity

How to prevent a public field from being copied through editor duplicate or copy/paste of an object?

Discussion in 'Scripting' started by s_guy, Oct 16, 2017.

  1. s_guy

    s_guy

    Joined:
    Feb 27, 2013
    Posts:
    102
    As title asks...

    This would be useful for fields that you need to be set explicitly per object or where you don't want certain field values replicated casually / accidentally.
     
    Last edited: Oct 18, 2017
  2. s_guy

    s_guy

    Joined:
    Feb 27, 2013
    Posts:
    102
    I ended up tracking all such objects in a scene manager, with ExecuteInEditMode for the component that needed to have special field handling. Then the object clears its own fields if it hasn't registered with the scene yet. The list of tracked objects is serialized, so all new instances besides those loaded from disk upon opening a scene get those fields cleared. This covers copy/paste, duplicate, prefab instances, etc.