Please help me understand prefabs and solve my problem with lookat

Hello there.

I feel like I don’t understand prefabs far enough. So I have this very simple code attached to my prefab.

    public Transform target;

    void Start () {

    }
   
   // Update is called once per frame
   void Update () {
        transform.LookAt(target);
   }

And it works like a charm when I drag prefab from project files to scene (hierarchy). But when it stays in project window then I can’t drag FPSController as “target” like I can when this prefab is in scene. It’s a huge problem, because my prefab are instantiated and random position (monsters in random generated maze). Could someone explain it to me, please? I mean, why I can’t drag something in inspector to set this public Transform target when it’s in project window and I can only when it’s already on scene?

If it changes anything - FPSController is already on the scene and FPSController is also a prefab, because I use it in multiple scenes.

I heard about workaround. I made script like this

    GameObject go;
    void Start () {
        GameObject go = GameObject.FindGameObjectWithTag("Player");
    }
   
   // Update is called once per frame
   void Update () {
       transform.LookAt(go.transform);
   }

But now I get error “Object reference not set to an instance of an object” and I don’t know how to handle it… Hope someone could spend few minutes and explain it to a newbie.

Best regards.

Okey, I did it. I am pretty sure that now I know how to deal with this error about object reference. Still I don’t understand why I can’t put gameobject through inspector when prefab is not in scene.

This is code i’ve wrote and it works now:

    GameObject go;
    Transform target;
    void Start () {
        go = GameObject.FindGameObjectWithTag("Player");
        target = go.transform;
    }
   
   // Update is called once per frame
   void Update () {
        transform.LookAt(target);
   }

If I understand your question correctly, why can’t you drag a reference in the inspector onto a prefab?

A prefab is just a class definition and not an actual object. Once you drag the prefab from the project window to the hierarchy, then you make an instance of the prefab, and you can drag a reference on to it. However, since most of your prefabs will be generated during runtime, you will not be able to have a preset reference, the instance will have to “look” for the reference, which is what you did with the find game object statement.

If I misunderstood your question, I’m sure JoeStrout will be by shortly to get things straightened out. He likes hanging out here with us beginners and he pretty much knows everything and like to help.

Lies, all lies! …Oh, wait, most of that is true. :slight_smile:

So here’s the thing about prefabs… they don’t exist in any particular scene; they exist in your project. They might be used in lots of different scenes. So, there is no way a prefab can reference something in the scene, because what would it do when you load that prefab into some other scene where the thing you’re referencing doesn’t exist?

So, the rules are: an object in the scene can only be referenced by other objects in the same scene (whether those are prefab instances or not doesn’t matter). An object in the project (such as a material, texture, or prefab) can be referenced by anything — in the scene or in the project — because it’s always available.

And all this means that yes, sometimes you have to hook up your object references at runtime, usually in the Awake or Start method.

But here’s a neat trick a lot of people don’t think about. It’s not just prefabs (in the project) which can be instantiated. You can also instantiate regular old objects in the scene. So sometimes, in a case like yours, I don’t use a prefab at all — instead I use what I call a “prototype”: an object in the scene, that just hangs out, deactivated, as a source for me to clone.

Think of it this way: Instantiate really does nothing more than clone an existing object and insert it into your scene. That object might be a prefab, which is an object that lives in your project. Or it might be an object in your scene. It doesn’t matter. BUT, a prototype (object in your scene), as opposed to a prefab, can have references to other objects in the scene.

So:

  • Instead of creating a prefab, just create an object in your scene. Configure it however you want, including making references to it from other objects in the scene, or vice versa.

  • Deactivate it (uncheck the checkbox at the top of the inspector). And mentally think of it as Jango Fett.

  • Whatever needs to instantiate these things, give it a reference to this Jango Fett object (just like it probably has a reference to the prefab now).

  • When you Instantiate it, remember to set .active = true on the fresh clone.

That’s it! The big advantage of prototypes is that they live in the scene. But of course the big disadvantage of prototypes is that they live in the scene. So, not so useful if you need to use them in multiple scenes.

But in many games, you need lots of clones of something only in one particular scene… so a prototype is a great way to go.

HTH,

  • Joe
1 Like

As always, thanks you for tips and helping me understand this a lot quicker. Have a nice day :slight_smile: