I have some prefabs that I use for level design. I’d like to “Break Prefab Instance” programmatically as soon as I drop the prefab into the scene. Is there an easy way to do this?
FYI, I want to do this because these particular prefabs are used as starting points for different level items and get heavily modified after dropping them into the scene. I need to ever “Apply” these changes.
Just use PrefabUtility.DisconnectPrefabInstance, but keep in mind it can only be used in an editor script or you have to surround the call with pre-processor tags #if UNITY_EDITOR
Here’s an example that should work:
// AutoBreakPrefabConnection.cs
using UnityEngine;
[ExecuteInEditMode]
public class AutoBreakPrefabConnection : MonoBehaviour
{
void Start()
{
#if UNITY_EDITOR
PrefabUtility.DisconnectPrefabInstance(gameObject);
#endif
DestroyImmediate(this); // Remove this script
}
}
I haven’t tested it yet, but in theory you would attach this script to the prefab itself (not to an instance of the prefab). When the prefab gets instantiated, it will run the Start method, it will beak the prefab connection and remove the script from the instance.