- Home /
How do I apply certain prefab changes with a Script?
I have a prefab that I changed the texture to with a script and I want that to apply to all the prefabs that are instantiated afterwards. How do I do this while the game is running? In C# please.
Answer by TonyLi · Aug 30, 2013 at 01:39 PM
If you're changing a prefab, you're running in the editor. In this case, use UnityEditor.PrefabUtility to manipulate prefabs. This only works in the editor (it relies on UnityEditor). When you change a prefab, new instances will automatically get the changed texture. If you need to update existing instances that were instantiated before changing the texture, iterate through the scene hierarchy to identify instances using PrefabUtility.GetPrefabObject and update the texture, or use PrefabUtility.ResetToPrefabState to reset everything (except transform) to the prefab state.
There are also some Asset Store products that will allow you to keep changes you make during play, so they'll stick around when you stop.
If you're running in a build, you're not changing the prefab. Prefabs are read-only in builds. In this case, create a master instance. Change the texture in that instance, and then instantiate copies of the master instance instead of the prefab.