- Home /
Restore prefab's name?
If a prefab instance in the scene is renamed, either by the user or automatically by duplicating the prefab in the scene, the name connection to the original prefab is automatically broken beyond repair, right clicking the name doesn't show a "Revert value to prefab" option, this makes it impossible to keep the original name since duplicating a prefab is the fastest way to work, now i want to rename some prefabs but renaming the master prefab won't change most of the prefabs in my scenes, I'd have to rename a thousands prefabs individually one by one if I ever wanted to change the prefab's name, is there no way to reset all instances to the original prefab name?
In short:
I want to rename all the prefab instances in my project by renaming the original prefab, which doesn't work on duplicated (or renamed) prefabs in the scene. I'd like to either prevent, or fix the broken name link.
To be fair, Unity usually warns you about when you're going to break a prefab instance. There is no returning it to its instance if your Undo Cache no longer contains the undo information. I can see why you'd want such a thing, but at the same time, you shouldn't go placing a thousand prefabs down if you aren't even done with the prefab, best practice is to make sure your object looks and works 100% how you want it to, BEFORE ever placing instances of it.
Another good thing to do, is if your prefab contains a common object structure, then you can separate the object structure and make a separate prefab that gets instantiated into the main prefab, allowing you to alter the object structure without altering the prefab.
I don't think you understand, I have a prefab named Plant_Fern_001 for example, i place it in my scene, then i want to put more Ferns so i duplicate it, but Unity renames the duplicates with (1), (2) and so on and instantly break the name reference to the original and there's no way to revert to the original name, ever. Now let's say later i want to rename the prefab to "Foliage_Fern_001" so i rename the original prefab, but none of the duplicates will get the name change, so i'd have to open all my scenes and rename the "Plant_Fern_001" prefabs to "Foliage_Fern_001" prefabs manually, which is crazy, there is a way to revert back pretty much any values by right clicking and choosing "Revert value to prefab" but that option isn't available for the name of the prefab.
do you mean the object name in the inspector? edit: if that's the case, what your asking is purely cosmetic. The only way I imagine this being an actual problem is if you're using GameObject.Find(""); which I would HIGHLY recommend NOT doing.
In the scene hierarchy or in the inspector, it doesn't matter, rena$$anonymous$$g or duplicating (which automatically renames) breaks the name connection to the original prefab, the name becomes bold in the inspector which means it is using it's own value, and that can't be restored to the original value.
I just don't see the purpose of why you need this. A bit of context could help. For example, if you're trying to get or use these names during runtime, there are many ways to simply trim the end off and just match the first part of the name. If you using Find, there are many better ways to store and get prefabs. can we get some context?
This is the only solution that seems to work so far but i don't like having to put that script on all the prefabs in my project https://answers.unity.com/questions/654151/changing-a-prefabs-name-via-the-editor-doesnt-appl.html
@$$anonymous$$henaB what version of Unity are you using? Unity keeps the reference to the prefab. I'm currently using 2017.3.1f1, but I remember earlier versions do too.
In screen cap 1, the first instance was created by hand and saved. FlyingObj(1) was added to the scene by dragging from the folder containing prefabs, and FlyingObj(2) was added by Ctrl+C
& Ctrl+V
of FlyingObj(1). The blue highlight indicates that it is indeed a prefab.
Please also note that the "Revert value to prefab" option is no longer in the context menu (I think), but is available in the inspector window ins$$anonymous$$d. See screen cap 2.
Additionally, reverting to prefab does not rename your cloned object. It just reverts the components and associated values to the prefab.
I'm using 2017.1, what i'm saying is that if you rename the original FlyingObj prefab in your project pane, FlyingObj in your scene will be renamed as well, bu not FlyingObj (1) and FlyingObj (2) because the name connection to the parent prefab was broken when you duplicated them and it added (1), now imagine you have thousands of FlyingObj placed in multiple scenes placed by duplicating them, and that you want to change that prefab name to FlyingVehicle, you'll have to rename them manually, there's no way to revert back, the Revert button doesn't revert to the original name.
Ok people don't seem to understand the point of this question so i try to clarify a bit.
Of course when we drag a prefab into a scene the prefab instance in the scene will usually have the same name as the prefab. If you duplicate the instance or drag it multiple times into the scene the name gets an additional increasing number added at the end. The actual problem is that during development it can happen that the name of an asset changes. However when you rename the prefab in the project of course all the prefab instances in the scene(s) won't be renamed and sill have the old name. For organisational reasons the instances should reflect the new assetname.
Imagine a prefab was named "Scene01_Prop001" but later it get a more concrete name like "Scene01_WoddenDesk". If you rename the prefab in the project the actual instances in the scenes will still be named "Scene01_Prop001", "Scene01_Prop001 (1)", "Scene01_Prop001 (2)", ...
Yes this is it, i have a large project with a lot of scenes and thousands of placed prefabs to which unity added (1), (2), (3) and so on, now i need to change some of the names and none of the instances will change to the new prefab name, i'm trying to find a way to prevent that link to be broken and to be able to rename all instances by rena$$anonymous$$g the master prefab, other values have a "Revert Value to Prefab" option on right click in the inspector but the name value doesn't, it seems impossible to restore the name value to the prefab value once unity renamed the asset on duplicate etc.
Answer by Bunny83 · Mar 26, 2018 at 03:38 AM
If you want to revert the names of prefab instances to the name of the prefab itself you can use this little tool i just created: PrefabNameRevert.
Just select one or more prefab instances and select "RevertName" in the "Prefab" menu.
Note that i removed my original script and replaced it with a much more simpler one.
Ahh Bunny83, thank you for taking the time to write this, unfortunately i already wrote an editor script that uses hierarchyWindowItemOnGUI to automatically change the prefab names, the rena$$anonymous$$g part isn't the issue, it can even be done by selecting all the prefabs and by changing their name in the inspector window all at once, the issue is that even if renamed back, they lose their name connection to the original (name is in bold in inspector and won't change if original is renamed), what i want to do is restore that connection, like a fresh prefab dragged from the project pane onto the scene which the name isn't in bold and will be renamed at the same time as the original prefab. I appreciate the work tho, the script works wonderfully
Ahh ok, yes you're lucky because recently Unity added some very useful methods to the PrefabUtility which allow you to modify any prefab modifications. This should do the trick:
public static void RemoveName$$anonymous$$odification(UnityEngine.Object aObj)
{
var mods = new List<Property$$anonymous$$odification>(PrefabUtility.GetProperty$$anonymous$$odifications(aObj));
for (int i = 0; i < mods.Count; i++)
{
if (mods[i].propertyPath == "m_Name")
mods.RemoveAt(i);
}
PrefabUtility.SetProperty$$anonymous$$odifications(aObj, mods.ToArray());
}
Note that this method will not work if you actually change the name in the same go as the actual saving / flush of the recent changes will happen after the control goes back to Unity. So if you change the name and remove the name property modification at the same time at the end when unity saves the changes it will store the name modification back into the file.
Though just running this method on a prefab instance should automatically turn the name back to the prefab name.
Hold on, i'm giving this a try, i'm unsure as how to call the RemoveName$$anonymous$$odification method, can you elaborate a bit?
Answer by BlueBananaProductions · May 01, 2020 at 11:59 AM
I wonder why the Editor doesn't let you do this through the Inspector. Every other game object property (that I am aware of) can be considered an "override" and inherits from the prefab and can be reverted. And you are allowed to name two or more objects in the scene at the same level of hierarchy the same thing...
Is the problem that users are more likely to want uniquely named objects, so doing "Apply All" on a prefab instance could ruin the naming in a whole scene? If that is the reason, it would be nice to have an option to "lock or unlock" certain properties for Overriding and Reversion.
Answer by Cornelis-de-Jager · Mar 26, 2018 at 04:20 AM
Hi @KhenaB,
Not sure why you want to rename the prefabs, but if it has to do with searchign for the prefab name in game you can use this:
void Start () {
this.gameObject.name ="SetTheNameHere";
}
Alternatively I found that selecting all the objects in the hieracrchy, right-clicking and say rename changes them all.
That doesn't restore the name link to the original prefab which is what i want, you'll have to rename all the prefabs in the project, possibly thousands across multiple scenes ins$$anonymous$$d of just rena$$anonymous$$g the original once.
Answer by g-aleksandrov · Nov 02, 2020 at 08:59 AM
You can change inspector to Debug view and there you can revert insance name.