Check the distance from a trigger to destroy distant prefabs

I am trying to make an auto-generating level where each piece destroys itself if you move too far away from it. But I can’t make the code work!

I want a trigger such that, when triggered, it checks the distance from itself to all prefabs of type X. If distance is too big, I want it to destroy those prefabs. In the prefab creation script I have included the line:

clone = Instantiate (prefab, transform.position, Quaternion.identity);

Then, in my GenDestroyer.js:

// var ObjToDestroy : Rigidbody;
var ObjToDestroy : GenNext.clone;
var MaxDistance = 40; 
// var distance = Vector3.Distance(ObjToDestroy.transform.position, Player.transform.position);


var distance = Vector3.Distance(transform.position, ObjToDestroy.transform.position);

//Debug.Log(distance); 
if (distance > MaxDistance) 
{ 
// Destroy(ObjToDestroy)
Destroy(ObjToDestroy);
}

It has problems, not the least of which it doesn’t recognize the NextGen.clone as a valid type. Any ideas??

Thanks for any help! It is much appreciated.

That’s because GenNext.clone isn’t a type, it’s a variable. Use ‘= GenNext.clone’ instead of ‘: GenNext.clone’.

Thanks! That seems to work, but now I can’t calculate the distance:

var ObjToDestroy = GenNext.clone; 
var MaxDist = 40; 
if( Vector3.Distance(transform.position, ObjToDestroy.transform.position) > MaxDist){
	
// Destroy(ObjToDestroy) 
Destroy(ObjToDestroy); 
}

Error on the Vector3.Distance(transform.position … error=‘transform’ is not a member of ‘int’