Why don't my Asset Bundles bundle?

HI all, I am trying to create an asset bundle in code by simply selecting a gameobject from the scene and hitting a button. My code creates a prefab version of the gameObject, attaches a couple of scripts and then bundles it up, ready for upload. However, I’m getting an error…

I am first creating a prefab like so:

prefab = EditorUtility.CreateEmptyPrefab("Assets/Prefabs/"+t.gameObject.name+".prefab");
EditorUtility.ReplacePrefab(t.gameObject, prefab, ReplacePrefabOptions.ConnectToPrefab);

…then I try to bundle it up using:

    Selection.activeObject=prefab;
    var selection = Selection.GetFiltered(typeof(GameObject), SelectionMode.DeepAssets);
    BuildPipeline.BuildAssetBundle(Selection.activeObject, selection, bundlesPath, BuildAssetBundleOptions.CollectDependencies|BuildAssetBundleOptions.CompleteAssets  );

This all works fine and a bundle is created in the right place but I get the error:Asset bundles can not include Editor Objects(Prefab).(?!) My asset bundles are just a gameobject with a couple of components attached (mesh renderer and a couple of my own scripts).

Any ideas?

@davidhorak

Instead of

Selection.activeObject=prefab;

do

Selection.activeObject=AssetDatabase.LoadMainAssetAtPath("Assets/Prefabs/"+name+".prefab");