Setting player to prefab variable problem

Sorry bout all the questions I’ve been posting lately but, I have this script attached to my enemies so that when they are instantiated from prefabs they are supposed to chase after my main character, but when I made a prefab for my main character and used that as the person that they follow after, but when i start the game, they dont chase him. So, my question is, is there a way to assign the variable leader (which is the person they follow) to my character using a find with tag or some of other type of script?

Anyone?? I’ve looked all over but cant figure out how to do this.

Are you saying that you are using the prefab reference as the target object for the enemies to chase? You are right - this won’t work. You need to use a reference to the instance that you create in the game, not the prefab itself.

If there’s only ever one “leader” for them to chase, you can always have a static instance variable in the leader’s script. Note that the Player’s Start method has to execute before you can use this variable.

Quick untested code example :

class Player : MonoBehaviour {
  public static Player instance;
  void start() {
    Player.instance = this;
  }
}

And then you can always say Player.instance (and Player.instance.gameObject etc) to find the Player object.

Thank you a lot for your help. But I use javascript so what would that be in javascript? I tried to change it to javascript myself but it didn’t work. Thanks.

If you have something like:-

var origPrefab: GameObject;
var clone: GameObject;

function Start() {
    clone = Instantiate(original);
}

// Other stuff
    ...

…where origPrefab is linked to a prefab, then you must set your leader to be the clone, not the prefab reference. The prefab doesn’t exist in the scene so it won’t get updated and will have no meaningful transform properties, etc.