Search Unity

  1. Check out the Unite LA keynote for updates on the Visual Effect Editor, the FPS Sample, ECS, Unity for Film and more! Watch it now!
    Dismiss Notice
  2. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  3. Improved Prefab workflow (includes Nested Prefabs!), 2D isometric Tilemap and more! Get the 2018.3 Beta now.
    Dismiss Notice
  4. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    Dismiss Notice
  5. Want to see the most recent patch releases? Take a peek at the patch release page.
    Dismiss Notice

Change prefab property in script without initialize and replace?

Discussion in 'Extensions & OnGUI' started by Thaina, Sep 12, 2018.

  1. Thaina

    Thaina

    Joined:
    Jul 13, 2012
    Posts:
    107
    Even it doable in prefab UI itself. I could not found a way to do it in editor script. The only way I could change prefab property is initialize it and replace it and delete it after even though I just want to change only one property of all prefabs in my project (I want to change a material of all renderer of each children in every prefabs)

    Are there anyway we could do that?
     
  2. Madgvox

    Madgvox

    Joined:
    Apr 13, 2014
    Posts:
    455
    Load all your objects using whatever method you want, then loop through them, modify each object, and call
    EditorUtility.SetDirty
    on it. Then call
    AssetDatabase.SaveAssets
    when you're done.
     
    Thaina likes this.
  3. Thaina

    Thaina

    Joined:
    Jul 13, 2012
    Posts:
    107
    @Madgvox Thanks you for your answer but today I had update project to 2018.3 was released and it seem there was a new `PrefabUtility.SavePrefabAsset` function that was really doing the job now
     
    Madgvox likes this.
  4. Madgvox

    Madgvox

    Joined:
    Apr 13, 2014
    Posts:
    455
    Oh cool, thanks for the info. It's easy to get behind on API changes if you primarily work in older versions of Unity. :p