What does it mean to link prefabs?

I have read over some of the best practices regarding prefabs but it is all very wooly in the area around managing multiple prefabs via composition.

So for example lets say you have a Player prefab, which contains a child model and a child behaviour tree. Now if you want the behaviour tree to be its own prefab then the moment you attach it to the player prefab it loses its connection.

So it was mentioned a few times to link prefabs, but what does this actually mean from an implementation point of view?

That is the article that specifically says it.

As best I can see, when they say “link” they mean a reference variable (sometimes called a “pointer”).

Something like this, which you might set in the inspector:

//JS
var jetpackAudio : AudioSource;

//C#
public Transform myTarget;

18 . Link prefabs to prefabs; do not link instances to instances.

The two example references I mentioned above serve different purposes:

  • “jetpackAudio” is probably an internal link to another component within the prefab.
  • “myTarget” is probably an external link to some other object in the scene.

In the one case, the jetpack audio reference will (probably) be the same every time you drop the prefab into a scene. The audio is part of the prefab. So that reference should probably be built into the prefab asset itself. You can do that by editing the prefab in the project panel, or by editing in a scene and selecting the “apply” button.

In the other case, the “target” is probably not part of the prefab. It doesn’t make sense to make it part of the prefab, because it’ll have a different target every time you drop it into a scene. That reference should probably be left blank, then set per-instance.

17 . Use separate prefabs for specialisation; do not specialise instances.

In this case, I think they’re just saying that it doesn’t make sense to have a single prefab for both “BlueEnemy” and “RedEnemy”. If they’re really different enemy types, they might as well get their own prefabs.

If need be, you could even create a third prefab that’s able to spawn either enemy type on demand. Maaaybe that’s what they mean by this “link” thing?

Getting your components to reference each other is a huge part of scripting. This guide is just giving some general tips for going about that, I think.