making a named script update

Hi there, I recently found a usful script on the unity script web page which gave this script to find the closest enemy to an object:

// Print the name of the closest enemy
print(FindClosestEnemy().name); 

// Find the name of the closest enemy
function FindClosestEnemy () : GameObject {
    // Find all game objects with tag Enemy
    var gos : GameObject[];
    gos = GameObject.FindGameObjectsWithTag("Enemy"); 
    var closest : GameObject; 
    var distance = Mathf.Infinity; 
    var position = transform.position; 
    // Iterate through them and find the closest one
    for (var go : GameObject in gos)  { 
        var diff = (go.transform.position - position);
        var curDistance = diff.sqrMagnitude; 
        if (curDistance < distance) { 
            closest = go; 
            distance = curDistance; 
        } 
    } 
    return closest;    
}

I am trying to use an adapt this script for use in a game to find the closest gun/weapon to me. I successfully managed to change the tag that it looks for however unlike this script I need mine to update constantly in-game

my script:

function Update () { 
    FindClosestGun();
    print(FindClosestGun().name);
}

// Find the name of the closest enemy
function FindClosestGun () : GameObject {
    // Find all game objects with tag Gun
    var gos : GameObject[];
    gos = GameObject.FindGameObjectsWithTag("Gun"); 
    var closest : GameObject; 
    var distance = Mathf.Infinity; 
    var position = transform.position; 
    // Iterate through them and find the closest one
    for (var go : GameObject in gos)  { 
        var diff = (go.transform.position - position);
        var curDistance = diff.sqrMagnitude; 
        if (curDistance < distance) { 
            closest = go; 
            distance = curDistance;
        } 
    } 
    return closest;
}

I tried to sort it by just calling the other function through an update script, when I play the game it changes the returned name once but then doesn't change back if I move back towards the original weapon.

I don't get any errors so I don't know why it does this

Thanks in advance

Scribe

This update system in game engines is usually done (meaning , for most engines) with a "hook" (a general term for putting things together). To hook your stuff to the Unity3D update routine you need to make your script derive from monobehaviour. If you do not do this you need to let an object that does do this forward each frame a call to an update inside the script in which that IS done.

Similarly done in XNA is the Game class or GameComponent/DrawableGameComponent which have an update method. If a class does not derive from Game it needs to have an update method which is called via some object reference from some class that is on the highest level of the chain all being called with the original update routine.

THIS IS A SAMPLE OF XNA CODE AS A REPLY ON A COMMENT IT WILL NOT FUNCTION IN UNITY3D AND SERVES NO OTHER PURPOSE BUT TO EXPLAIN A CONCEPT

//Some dependancies and library stuff
using 'stuff';

public class Main : Game
{
    //constructors load unload content draw and what not
    junk;

    /// <summary>
    /// Allows the game to run logic such as updating the world,
    /// checking for collisions, gathering input, and playing audio.
    /// </summary>
    /// <param name="gameTime">Provides a snapshot of timing values.</param>
    protected override void Update(GameTime gameTime)
    {
        someObjectReference.Update()

        base.Update(gameTime);
    }
}

someObjectReference (your other script) would now be able to receive this once per update. The same can be done if you skip this and just derive that object from the Game class ( Or GameComponent OR DrawAbleGameComponent which are parts of the Game class which is the root updater)