- Home /
Breaking a prefab doesn't break it completely?
Hello.
When i instantiate an object in a scene and break the prefab at GameObject -> Break Prefab Instance, then its not more blue in the hiearchy list, however, in the Inspector, i can still see that i can apply to the prefab.
So my question is, how do i break the prefab from my instance competely?
Answer by Bunny83 · Dec 06, 2013 at 01:37 AM
As far as i know there's no way in the editor besides using Instantiate from an editor script to clone the object. I haven't tried it recently, so that might has been changed as well but used to work pretty well. When they introduced the "Break Prefab connection" button it fully disconnects the instance. I guess that most people complained that it's not possible to reconnect it. They renamed the button to "Break Prefab Instance" and now it's possible to reconnect to the prefab. So this is a quite recent change and i guess it's on purpose ;)
You can try PrefabUtility.DisconnectPrefabInstance in an editor script, but i guess that does the same as the menubutton. Instantiate used to do the job (actually it was a problem until we get PrefabUtility.InstantiatePrefab which lets you instantiate a prefab in the editor and preserve the prefab connection).
Answer by SilverStorm · Nov 22, 2016 at 06:33 AM
Breaking a prefab requires you to select the game object in the scene and then going to the game object menu > break prefab connection. Then you can now safely delete the prefab asset in your project folder without your scene game object turning red.
If you do not want to delete the asset from your project folder then I think the revert option may likely remain in view. However I have not tried to rename my project folder asset after the breaking has occurred....their may still be a way.
Answer by baci · Oct 14, 2015 at 02:48 PM
There seems to be no direct solution, which is why I created a small script that I attach to prefabs that I don't want to be changed from the hierarchy: It first breaks the prefab connection, then creates a temporary prefab in the Assets folder and connects the current object to that prefab. On Disabling, the prefab gets removed again. Seems very hacky, but works in my case.
Here is the code:
using System.IO;
using UnityEngine;
using UnityEditor;
/// <summary>
/// This script can be attached to any prefab and prevents the prefab from being edited
/// and saved in the inspector view.
/// This can be desired when changes to instances should not affect the prefab of the object.
/// </summary>
[ExecuteInEditMode]
public class DisconnectPrefabOnSpawn : MonoBehaviour
{
private Object tmpObject;
private void OnEnable()
{
#if UNITY_EDITOR
if (!File.Exists(Application.dataPath + "/TMPPrefab.prefab"))
{
PrefabUtility.DisconnectPrefabInstance(gameObject);
tmpObject = PrefabUtility.CreatePrefab("Assets/TMPPrefab.prefab", gameObject, ReplacePrefabOptions.ConnectToPrefab);
//Destroy(this);
}
#endif
}
private void OnDisable()
{
#if UNITY_EDITOR
if(AssetDatabase.Contains(tmpObject))
AssetDatabase.DeleteAsset("Assets/TMPPrefab.prefab");
#endif
}
Answer by Bas-Smit · Mar 08, 2016 at 05:43 PM
public static void RemoveDisconnectedReferences(string scenePath)
{
var lines = File.ReadAllLines(scenePath);
var sb = new StringBuilder();
var inPrefab = false;
var removingDisconnectedPrefab = false;
for (int i = 0; i < lines.Length; i++)
{
var line = lines[i];
if (line.StartsWith("---"))
inPrefab = lines[i + 1] == "Prefab:";
if (inPrefab)
sb.Append(line + "\n");
else
{
if (line.StartsWith(" m_PrefabParentObject:"))
{
sb.Append(" m_PrefabParentObject: {fileID: 0}\n");
removingDisconnectedPrefab = true;
}
else
{
if (!(removingDisconnectedPrefab && line.StartsWith(" type:")))
sb.Append(line + "\n");
removingDisconnectedPrefab = false;
}
}
}
File.WriteAllText(scenePath, sb.ToString());
}
Answer by karmington · Feb 14, 2018 at 11:12 PM
Prefab breaking Editor script solution also here, confirmed working at Dec 20, 2017 : https://forum.unity.com/threads/breaking-a-prefab-instance-for-good.501919/
Your answer

Follow this Question
Related Questions
Changing the Variables of An Instanced Script 2 Answers
How to refer to a collision between clones of the same prefab? C# 1 Answer
Updating a variable on a script in an instanced object 1 Answer
What is the difference between grey and blue prefab ? 1 Answer
Importing instances via FBX and replacing with prefabs 1 Answer