Create a new prefab from scripting

Guys,

Is it possible to create a new prefab from a GameObject in the scene at runtime?(from scripting, not manually)

prefabs can not be generated at runtime at all, the can only be generated in the editor

and what about in the editor at runtime? can it be done.

if it is not clear: I press the play button and it creates the prefab

EditorUtility.CreatePrefab and ReplacePrefab can be used to create and update prefabs from an editor script. However, this doesn’t happen during gameplay. It may be possible to achieve what you want to do in a different way, though. Can you give any information about why you need to create the prefab?

Not sure what you’re trying to achieve, but maybe you could just create a new gameobject and call DontDestroyOnLoad on it. Then use that instead of a prefab. If you want it to persist after the app is closed you’ll probably need to serialize it yourself using PlayerPrefs.

if you press play you are in the game / runtime mode, not the editor mode. in that mode you can’t have editor scripts and functionality and as such not create a prefab.

You would need to write editor scripts as andeeee points out with the corresponding functions to use.

Well, but couldn’t you use a game object as a prefab? Clone, etc?