- Home /
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 $$anonymous$$m wrote a rena$$anonymous$$g 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.
Answer by vexe · Mar 06, 2014 at 02:19 PM
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)
Thanks, very useful example. One question: Is it possible to achieve the same, but without adding an extra script to the GameObject? I mean somehow extend the inspector via Editor scripting, that every GameObject in the scene execute this "prefab name check" while we inspect the GameObject in Editor. (According to this topic, it's not a good idea to replace internal classes "GameObjectInspector" or "TransformInspector", because it overrides Unity's implementation of their custom inspector. $$anonymous$$aybe there is a solution to extend them, not to replace).
Your answer
Follow this Question
Related Questions
How can I link a GameObject instance to a script variable in a Prefab using the editor? 1 Answer
changing a bool in a prefab doesn't change in it's instance. 2 Answers
Gameobject missing camera when instance prefab 3 Answers
Destroy an instance of a prefab 1 Answer
Why do prefabbed meshes go missing whenever I pull an update from Unity Collab? 0 Answers