Homing Missile target with "Find Objects With Tag" not working?

I’m trying to make a homing missile. The way I have the spells in my game, when you press the fire button, it instantiates a prefab. That prefab then does whatever the spell or ability is supposed to do, and then destroys itself. Each prefab is generic and every player casts the same prefabs. The prefabs are also fired from a “Gun” null object that’s parented to the player, so there’s a slight disconnect in the hierarchy. To help with that, the “Cast” button creates each prefab as a child of the “Gun” null object.

Hopefully that makes sense. The reason that’s all relevant, is because when this homing missile is fired, it has to find a target. It sifts through the “Find Objects with tag” code that unity script reference offers, and SHOULD then assign the “closest” to the “target”. But it doesn’t. I’ve tested this code with a hard-coded target, and it works just fine. I’ve tested it with a more basic “just find anything with the player tag” and it works there too. It’s just the script reference “FindClosestPlayer” function that doesn’t seem to be working.

Or maybe I’m just trying to interact with it the wrong way. Either way, I keep getting the following error:

“NullReferenceException: Object reference not set to an instance of an object
Boo.Lang.Runtime.RuntimeServices.InvokeBinaryOperator (System.String operatorName, System.Object lhs, System.Object rhs)
Chase.Update () (at Assets/Other/Spells/Chase.js:23)”

And I’m stumped at this point. Please peruse my code and tell me exactly how and why I am an illiterate moron.

var target : GameObject; //the missile's target
var moveSpeed = 3; //move speed
var rotationSpeed = 3; //speed of turning
var lifeTime : float;
var myTransform : Transform; //current transform data of this enemy
function Awake()
    myTransform = transform; //cache transform data for easy access/preformance
function Start()
	Destroy(gameObject, lifeTime);
function Update () {
    //rotate to look at the player
    myTransform.rotation = Quaternion.Slerp(myTransform.rotation,
    Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed*Time.deltaTime);
    //move towards the player
    myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;

function FindClosestPlayer() : GameObject { 
    // Find all game objects with tag Player 
    var gos : GameObject[]; 
    gos = GameObject.FindGameObjectsWithTag("Player"); 
    var distance = Mathf.Infinity; 
    var position = transform.position; 
    // Iterate through them and find the closest one 
    for (var players : GameObject in gos) {
     if (players == gameObject.transform.root) continue; 
       var diff = (players.transform.position - position); 
       var curDistance = diff.sqrMagnitude; 
       if (curDistance < distance) {  
         closestPlayer = players;
         distance = curDistance; 
    return closestPlayer; 
    target = closestPlayer;

You only call FindClosestPlayer once in Start(), so if another player isn’t found at Start(), closestPlayer is probably null which is why 23 breaks down. You’ll need to move the call to FindClosestPlayer in a coroutine, invoke, update, etc. and you should test that closestPlayer is not null

is it because you return before setting target = closestPlayer;

should it not be …

target = closestPlayer;
return closestPlayer;

also add

target = FindClosestPlayer();

in Start()

There are two errors beside the above in the code line 22 should be

Quaternion.LookRotation(target.transform.position - myTransform.position), rotationSpeed*Time.deltaTime);

and line 44 var has not been declared, so need to add to the top

var closestPlayer : GameObject;

Then it should work.