What is the difference between a prefab and a gameobject?

Hello,

I'm having a hard time understanding the concept of prefabs...What is the difference between a prefab and a gameobject? I read the manual a few times but I still don't get it.

Before I further explain, I want you to know I come from a mapping background (Valve Hammer Editor) which used to have prefabs as well. Prefabs were simply premade objects which you could place in a level, saving you from modeling something over and over again (for example a house or tree).

In my game I have 20 different models for a strategy game (Tanks, Planes, Buildings, etc) Do I have a to make a prefab for every one of them? Why (not) ?

Is a prefab an instance of a gameobject? Why were prefabs made available to developers? Why not just use gameobjects all the time?

To initialize a prefab, I must drag and drop a GameObject from Hierarchy view to the prefab in Project View. Why? I can also drag and drop a GameObject from the Project View onto the prefab. What is the difference, what happened now in comparison with dragging from the scene?

Thanks in advance! Nick

Your confusion is understandable, since game object and prefabs amount to different ways of storing the same information, and you can in fact create one from the other (both ways!)

A game object is a bag of components, and lives in the scene -- it shows up in the Hierarchy and is serialized into Unity's custom database format in the Library folder. Game objects can have other distinct game objects as children.

A prefab is a copy of a game object converted into a reusable asset -- it shows up in the Project folder and is serialized as a file on disk. Prefabs can contain a hierarchy of game objects.

As you know, you can create a new game object directly, but you can also create one by instantiating a prefab as a game object, by dragging it from the Project view into the scene. Changes made to the prefab will affect all the instances. Instances can have their values tweaked to override the prefab, but if you add or remove a component the prefab connection is broken.

To create a prefab, you first create an empty prefab (right-click in the Project view) and then drag a game object (from the Hierarchy) onto it.

See also this question about components and game objects for somewhat related info.

Prefabs can be a single Game Object, or a hierarchy of them. In theory, you could load an empty scene, and then solely instantiate prefabs to build a scene. But setting things up in the Hierarchy/Scene views is certainly a lot easier!

A Game Object, or hierarchy of Game Objects, in the Hierarchy view, should be made into a prefab if you want to be able to recreate it at will. If you're only going to have something be static, in one scene, then there's no point in making a prefab of it.

Prefabs live outside of scenes. Instantiated prefabs live in scenes. The Project View represents all of the assets that might be used in scenes. The Scene View and Hierarchy View display everything in one scene. A scene requires no prefabs. If everything you have is unique, prefabs are not helpful to you, but that's not very common.

I would say that prefab is simply a gameobject hierarchy that’s not part of a scene, but it’s an asset. But from scripts, if you have a reference, there’s no big difference if you refer to a gameObject / component that’s a prefab or a part of scene. Just that prefabs don’t receive messages from the engine.

IMHO simplest definition: Prefab is a container which could be empty or contain any number of gameobjects.

We use prefabs to keep gameobjects in the library not on the scene(for better access and game quality) and when we need them we put gameobject from prefab to the game.

I found all the answers useful. I am checking this on 2018 so It doesn’t matter if the answer was written on 2011 or 2013. Thank you for collaborate!

As a guy with the Adobe Flash background, I’d find the simplest explanation of a prefab to game objects relation like this:
Prefabs are like classes, and game objects are like instances of those classes. If to go down to pure Flash concepts, prefabs are like symbols in the .FLA file library, and game objects are like the movie clips — instances of those symbols.

Another helper for understanding the relation could be thinking of the prefabs as templates for future game objects.

But these two are similar, as classes in general are templates for future instances.

Drag’n’drop behaviour and what’s created based on/from what is simply details of Unity’s implementation of the concept.

Prefabs allows to reuse same data again and again. Using prefabs reduce build size and build time and also make collaboration easy

Let’s assume you have 10 Scenes that have the same Camera with attached a player controller, if you want that GameObject to be exactly the same across scenes it is better it becomes a prefab, so that later you change your mind about that GameObject, you just need to change the prefab, and all your changes to the prefab are reflected into all your 10 scenes.

If you have a scene with 100 GameObjects and none of them is prefab, if you change 1 GameObject and another develoepr changer another GameObject you will have to do a merge of the scene which is usually easy if you know what you are doing, but will make you lose times and there can be lots of problems anyways.

Again if all 100 GameObjects were Prefabs, you both change prefabs and no need to merge the scene.

Also when prefabs starts to get bigger, using them allows to have scenes with small sizes, if you reuse a prefab many times, you save considerable space for each scene which at the end turns in less space used and faster build time.

A prefab is simply a way of saving a game-object’s (or hierarchy of game-objects) configuration so that you can re-use it.

Let’s say you have a model of a tree with no materials, collision volume or rigid body applied. If there was no such thing as a prefab, you’d have to apply the correct materials, and add a collision volume and rigid body to every tree in your scene. This, of course, could introduce inconsistencies as you may apply different sized collision volumes to some of your trees, or accidentally apply the wrong materials to others.

This is where prefabs come in. You’d simply add your tree model to your scene, apply materials, add and configure the required collision volume and rigid body, and then drag the resulting game-object into your assets folder. The result will be a saved copy of this object, so you can now insert your “tree” prefab without having to go through the process of configuring each and every one!