help with enemy instantiate script.

so i have this script and it instantiates an enemy every 10sec, but i want it to instantiate when the player is within a certain distance. I am new at scripting so any help will be great.

Thank you!

~ Sam

var prefab : GameObject;

while(true)
{
go = Instantiate(prefab);
go.transform.position = new Vector3(Random.value * 10, 0, Random.value * 10);
var offset = transform.position - target.position;
yield new WaitForSeconds(10);
}

You can add an if statement after the while loop to check the distance. Make note of the second yield because Unity will crash without it.

var prefab : GameObject;
var player : Transform;
var spawnDistance : int = 10;

function Start () {
    PlayerCheck();
}

function PlayerCheck () {

    while(true)
    {
        if(Vector3.Distance(player.position, transform.position) < 10) {
            go = Instantiate(prefab);
            go.transform.position = new Vector3(Random.value * 10, 0, Random.value * 10);
            yield new WaitForSeconds(10);
        }
        else yield;
    }
}

once you have a handle of the players transform you can use Vector3.Distance http://unity3d.com/support/documentation/ScriptReference/Vector3.Distance.html to find out how far away they are.