I suspect that your problem has a different cause: since there are several Block objects around, the trigger Enter and Exit events may get fooled: coll becomes true when any Block enters the trigger, and turns to false when any Block exits - no matter how many Block objects are inside the trigger.
Anyway, you could try something like @DaveA suggested:
var coll = false;
var stay = false;
function OnTriggerStay(other: Collider){
if (other.CompareTag("Block")){
stay = true; // set stay if any Block inside the trigger
}
}
function FixedUpdate(){
if (stay){ // if any Block inside the trigger...
coll = true; // set coll
stay = false; // and prepare flag for next cycle
} else { // if stay not set, no Blocks are inside
coll = false; // thus clear coll
}
}
This code sets coll when the Block enters the trigger, and clears it when the Block exits. You must use FixedUpdate to control coll because Trigger events occur in the physics cycle. Use Update to do the rest - including to read coll.
I suppose that coll will become false only when no more Blocks are inside the trigger (I didn’t test this).
EDITED: Triggers are good for detecting when things enter and exit, but if something is created or teleported inside the trigger, nothing happens.
There are several ways to detect if there are objects in a given volume, but I think the easiest is to use Physics.OverlapSphere: you inform radius and position of an imaginary sphere, and the function returns all colliders that touch or are inside it. Since other objects - like the ground or scene details - may be also detected, you must tag each cube you create with a specific word - “Cube”, for instance - so you can check if any “Cube” is returned by OverlapSphere and ignore the rest.
Place the code below in your cube creator script, and call the function SpawnCube when you want to create a new cube. The routine checks if some object tagged “Cube” intersects the sphere, and aborts if any one found, otherwise it instantiates the object referenced by prefab in the current position.
var prefab: GameObject; // drag your cube prefab here
var radius: float = 2; // cube-free sphere radius
function SpawnCube(){
var cols = Physics.OverlapSphere(transform.position, radius);
for (var col: Collider in cols){
if (col.tag == "Cube"){ // if any cube intersects sphere...
return; // abort instantiation
}
}
// no Cube found, instantiate new cube:
var cube = Instantiate(prefab, transform.position, transform.rotation);
cube.tag = "Cube"; // and tag the new cube as "Cube"
}