Can references be applied to prefabs? Workarounds?

Hi all
I have various enemy types set up as prefabs. these enemies have scripts with public variables that I use to store references to other objects in the scene through drag n’ drop (e.g. a favTarget variable on a TargetSeeker script). The intention is to keep things as generic as possible for game design flexibility.

a) Now it seems I can’t apply these drag n drop references to the original prefab (which could make sense since in other scenes the referenced objects might not exist).
Is that true?

b) if it is possible, how do I do that? pressing “Apply” doesn’t do it.

c) If it’s not possible, how can I store these object references in prefabs in a different way, without hardcoding them, so my various scripts retain their flexibility?

thanks for any advice and pointers!

You can’t store references to scene objects in prefabs. You can only store references to “assets” (prefabs, textures, anything in the project view). Usually you create both objects as prefabs and then drag both into the scene; this causes them to link to each other in the scene.

So if you have a Box that wants to know about a Ball, you create a Ball prefab and you create a Box prefab, you drag the Ball prefab into the Box prefab, and then you drag both prefabs into the scene at the same time.

For anyone still searching for an answer to this problem (i.e., an enemy prefab that must reference the player in order to chase the player), I found that a simple line of code on the prefab enemy solves the problem:

  1. Leave the enemy’s editor reference slot to “player” empty (as it will be any time that enemy is instantiated).

  2. As most enemy follow scripts use “public Transform player” at the very start to tell the enemy what point to follow (thereby creating the empty reference slot in the editor which is usually filled manually with a drag and drop), you just need to add this line to the “void Start () {” function:
    “player = GameObject.FindGameObjectWithTag (“whatever your tag is”).GetComponent();”.

This works every time for me and can easily be altered slightly to fit most any situation. Try it and you’ll see the empty slot magically fill with the necessary reference upon start.