How could I create a prefab from an exsisting gameobject at runtime?

I need to create a prefab from a gameobject at runtime, but without changing the gameobject. I also need this prefab to contain all children and grandchildren, unless the current prefab system already does this.

As others have said, I don’t think it can be done exactly the way you want. I know you said you weren’t interested in workarounds, but the best solution I have is making a clone of the source GameObject and parenting this snapshot under a disabled GameObject, so that it doesn’t update or render. Then you can clone this elsewhere in the scene just as you would a prefab.

There is no way to create prefabs in a built player. You can do it in the editor, but that doesn’t seem to be what you’re looking for.

I need to create a prefab from a gameobject at runtime, but without changing the gameobject.

You can certainly use Instantiate() to duplicate objects. You could disable those objects so that their state never changes. You could keep them between scenes with Object.DontDestroyOnLoad. You could use some static references to keep a “singleton” reference. In practice, that’s the closest you can get to a “runtime prefab”.

I also need this prefab to contain all children and grandchildren, unless the current prefab system already does this.

Prefabs contain one GameObject and all of its children (with the minor caveat that its children cannot be “nested” prefabs).

If you Instantiate() an object, the clone will contain copied children and components.