ProfileUtility.InstantiatePrefab returns null in Unity 4

I’m working on a tool that will build a 2D level based on info from a text file.

When the text file is imported, I want to create instances of the game objects that retain their connection to a prefab.

In Unity 4, when I do this:

Object prefabRoot = EditorUtility.GetPrefabParent (sprite);
GameObject dupe = EditorUtility.InstantiatePrefab (prefabRoot) as GameObject;

the dupe object is null.

The documentation seems to suggest that it shouldn’t return null.

If InstantiatePrefab will always return null, is there any other way to accomplish what I’m trying to do?

The answer is to change:

GameObject dupe = EditorUtility.InstantiatePrefab (prefabRoot) as GameObject;

to:

Object dupe = PrefabUtility.InstantiatePrefab (prefabRoot);

And then everywhere I have to use dupe, I have to explicitly cast it as a GameObject type.

I keep bumping into this issue as well. What seems to work for me is to make sure that the type of the public variable is the type you intend to use. Then redo your drag and drop in the inspector. Finally, make sure your Instantiate() is being assigned to that same type.

Don’t have a public GameObject or Transform that you drag your prefab to in the editor, and then try to instantiate it as the class name of your prefab and/or assign it to something that is the type of your prefab.