Hi, I’m trying to write an editor script that replaces a prefab with a new one.
The script has an Object variable called MapObject
MapObject points to a prefab before running the script.
If I then replace the prefab during the program, the MapObject variable gets set to Nothing after it is done.
Ok, that is to be expected, so I set the Object variable back right after the save operation to point it to the new prefab and it is there in the inspector.
The problem is when I stop the program, the MapObject variable gets set to Nothing again, but I want the MapObject to stay pointed to the new prefab even after the program ends.
Any ideas? Here is the code I use for replacing the prefab and then setting the variable to the new prefab.
string localPath = "Assets/Resources/Prefabs/" + theMap.gameObject.name + ".prefab";
Object prefab = AssetDatabase.LoadAssetAtPath(localPath, typeof(GameObject));
if (prefab != null) {
if (EditorUtility.DisplayDialog("Are you sure?", "The prefab already exists. Do you want to overwrite it?", "Yes", "No")) {
PrefabUtility.ReplacePrefab(theMap.gameObject, prefab, ReplacePrefabOptions.ConnectToPrefab);
}
}
else {
prefab = PrefabUtility.CreateEmptyPrefab(localPath);
PrefabUtility.ReplacePrefab(theMap.gameObject, prefab, ReplacePrefabOptions.ConnectToPrefab);
}
prefab = AssetDatabase.LoadAssetAtPath(localPath, typeof(GameObject));
MapObject = prefab;
Update: Ok, I tried a few different things. Turns out changing ReplacePrefabOptions.ConnectToPrefab to ReplacePrefabOptions.ReplaceNameBased solved the problem.