Prefeabs do not remember Inspector selected GameObjects. Why?

Hi,

I have some prefabs that have a number of attached scripts. These require to know the location of a GameObject, and I was using the inspector to select the required GameObject. For example:

var target : GameObject;

When the prefab is placed into the Scene, I can drag and drop the required GameObject onto the script no problem. However I cannot apply the selection to the prefab. Also I can drag the GameObject into the prefab in my resources folder.

Is this normal behaviour as it seems counter intuitive to me?

If it is normal behaviour, am I best to refer to my target GameObject using the find GameObject, etc commands, or is there a more efficient way to do this?

I ask as I dread to think how much typing would have to be done if I changed the name of the target component / gameobject etc.

All words of wisdom welcome!

Thanks

Paul

If the prefab would remember an object from the scene, the prefab would be limited to only this scene. Even if you had another instance of your target in another scene it would be a different gameobject. Thus prefabs will only remember other prefabs as used for instantiating i.e…

I’d suggest to use Find/FindWithTag once during start. That is not more than a line of code. Just make sure your target has a unique name/tag. Also don’t use those functions every frame since they are expensive.