Changing a prefab's name (via the editor) doesn't apply the new name to the prefab's instances?

I just noticed that if I change the name of a prefab, the new name won’t be applied to the prefab’s in-scene instances. That’s not what I would expect. Same thing going the other way around, if I change the name of one of the instances of the prefab and hit apply, the prefab’s name will remain.

So how can I reflect the changes to the prefab instances when I rename it?

Thanks!

Someone on our team wrote a renaming extension into unity because we had the same issue. I can ask him about letting you use it but not sure what he'll say since he is planning on packaging that with some other tools to sell on the asset store. Saying this just to let you know that's the only way around it we found.

Hey @iHaveReturnd I noticed after a while that sometimes it works, sometimes it doesn't - I explain when it doesn't in my answer. Thanks for your offer but I wasn't looking for a hack actually (it's very trivial to come up with a work-around), I thought there was something already built-in, a setting or something.

1 Answer

1

It seems that, if I manually change the name of one of the instances of a prefab, and then rename the prefab itself, the new name won’t be applied to the instance that I renamed.

If I don’t mess around like that, i.e. I be a good boy, instantiate the prefab, (by drag-n-drop or code) - and then rename the prefab (without messing with its instances names) - everything will work nice and cozy.

I didn’t like this really, so I wrote this small hack, which forces all the instances of the prefab, to have the same name as the prefab - but it forces you to rename from the prefab, not the instances.

Just stick it onto your prefab’s instances that you want their names to be connected to the prefab’s name:

using UnityEngine;
using UnityEditor;

[ExecuteInEditMode]
public class PrefabNameUnifier : MonoBehaviour
{
	[SerializeField] [HideInInspector] GameObject prefab;
	void OnEnable()
	{
		if (PrefabUtility.GetPrefabType(gameObject) == PrefabType.PrefabInstance) {
			prefab = PrefabUtility.GetPrefabParent(gameObject) as GameObject;
			EditorApplication.update += CheckForNameChange;
		}
	}
	void CheckForNameChange()
	{
		if (prefab.name != name) {
			print("Changing instance name from: " + name + " to " + prefab.name);
			name = prefab.name;
		}
	}
}

Since this MB will be used only in the editor, you have to wrap everything with “#if UNITY_EDITOR” “#endif” - one might think it’ll be better to just place it in the editor folder, but unfortunately it won’t be visible then (one can’t attach it to a GO)