Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question Changing Preab values using System.Reflection

Discussion in 'Prefabs' started by lukasleder, Jul 14, 2021.

  1. lukasleder

    lukasleder

    Joined:
    Sep 7, 2017
    Posts:
    16
    Hello,
    I've been trying to inject fields inside of prefabs using reflections but nothing I've tried so far is working.

    I've tried two things, editing the prefab instance and using
    PrefabUtility.RecordPrefabInstancePropertyModifications().

    And Editing the prefab asset using
    PrefabUtility.LoadPrefabContents();
    PrefabUtility.SaveAsPrefabAsset();
    PrefabUtility.UnloadPrefabContents();

    None of these methods seem to work with
    FieldInfo.SetValue() though as there are no changes on the prefabs or prefab instances after executing the code, I have debugged the code and the values are definitely being set, am i missing a method for the prefab to realize, that these values have changed?

    The code works fine outside of prefabs btw.

    Thanks in advance!
     
  2. Xarbrough

    Xarbrough

    Joined:
    Dec 11, 2014
    Posts:
    1,188
    If you would post a complete snippet I would test it out and see how I would make it work. Maybe it's possible to call EditorUtility.SetDirty in this case. Or, the preview scene must be marked as dirty, not sure what the serialized property system does internally when dealing with a prefab.