Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Force serializable field to save on prefab

Discussion in 'Scripting' started by angrypenguin, Jul 8, 2013.

  1. angrypenguin

    angrypenguin

    Joined:
    Dec 29, 2011
    Posts:
    15,509
    I have a problem with prefabs with serialized fields. Here's the scenario:

    - I have a script which sets a value in a private field marked as serializable. Setting the value manually is not feasible, as I will have potentially hundreds of these in a scene, but I need the value to be serialised.
    - The script is set to [ExecuteInEditor] and, on Awake(), successfully sets the correct value in the field if it is not previously set from a serialized value.
    - This works correctly for all objects except prefabs. Prefabs work if and only if I select each one and manually touch the value in the Inspector after it has been set. If I do not, the value is displayed as if it were the default value and does not get serialized, causing errors the next time the scene is loaded.

    Is there any way I can force the prefab to recognise that this value needs to be saved?
     
  2. angrypenguin

    angrypenguin

    Joined:
    Dec 29, 2011
    Posts:
    15,509
  3. cubrman

    cubrman

    Joined:
    Jun 18, 2016
    Posts:
    403
    Cannot imagine how you made it work. This problem is one of the mind-boggling mysteries of Unity.
     
  4. Jorhoto

    Jorhoto

    Joined:
    May 7, 2019
    Posts:
    99
    I found it also easy to solve via EditorUtility.SetDirty.