Detecting if a certain tag near another tag?

I am trying to figure how to find the distance between two prefabs that are created. Here is my current script…

var chaseDist: float = 5;

function Update () {

var player = GameObject.FindWithTag("Block");
var playerDist = Vector3.Distance(player.position, transform.position);
	if(playerDist <= chaseDist)
	{
		Debug.Log("Colliding");
	}
}

Can anyone help a confused and lost kibbles? x)

You can check if player was found (and if it’s a different object) before using it:

var chaseDist: float = 5;

function Update () {

    var player = GameObject.FindWithTag("Block");
    // check if some object found, and if it's not this one
    if (player && player != gameObject){
        var playerDist = Vector3.Distance(player.transform.position, transform.position);
        if(playerDist <= chaseDist)
        {
            Debug.Log("Colliding");
        }
    }
}

EDITED: Things are easier if this script is in the Block creator. You can use FindGameObjectsWithTag like this:

function Update(){
    var blocks = GameObject.FindGameObjectsWithTag("Block");
    for (var player in blocks){
        var playerDist = Vector3.Distance(player.transform.position, transform.position);
        if(playerDist <= chaseDist)
        {
            Debug.Log("Colliding");
        }
    }
}

But FindGameObjectsWithTag is a slow operation (like all Find routines), and should not be called every time in Update. You can do that if your game has few objects (< 100, for instance), but it may slow down the frame rate if there are lots of objects in scene - every time the function is called, it must check each object to know if its tag is what you’re looking for.

A better alternative would be to create an empty object and make it the parent of each Block right after it has been instantiated - you would have to check only their children, instead of all objects in scene:

var allBlocks: Transform; // drag the empty parent object here
var chaseDist: float = 5;

function Update () {

    for (var child : Transform in allBlocks){
        var playerDist = Vector3.Distance(child.position, transform.position);;
        if(playerDist <= chaseDist)
        {
            Debug.Log("Colliding");
        }
    }
}

And for this to work you must only add a instruction to the creation routine (in this same script!):

    ...
    // instantiate the block as before, but save its reference in a variable
    var newBlock = Instantiate(...);
    // this instruction makes the new block a child of allBlocks
    newBlock.transform.parent = allBlocks; 
    ...