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.
