Drilling-game script's logical bug.

function Update() {
if(Input.GetButton(“Down”) && CanBreak){
DrillDown();
CanBreak = false;
Debug.Log(“Pressed Down”);
}
}

function DrillDown(){
	Debug.Log("DrillDown");
    
    var blockName : String = "GridBlock" + parseInt(Mathf.Round(player.transform.position.x)) + "-" + parseInt(Mathf.Round(player.transform.position.y - 0.5));
	var block : GameObject = GameObject.Find(blockName);

    if(block == null){
        Debug.Log(blockName + " doesn't exist!!");
    }else{
    	yield WaitForSeconds(BreakDelay);
    	if(Input.GetButton("Down")){
    		if(block.tag == "Dirt"){
				Debug.Log("Dirt +1");
				GameObject.Destroy(block);
				Debug.Log("Destroyed");
				CanBreak = true;
			}
			if(block.tag == "Stone"){
				Debug.Log("Stone +1");
				GameObject.Destroy(block);
				CanBreak = true;
			}
		}else{
			CanBreak = true;
		}
	}
}

Hello, I have a problem with my script. When I press down, it drills a block but it doesn’t set CanBreak to true. It just won’t let my drill another block. I know it has something to do with the boolean CanBreak. My debug log says this:

I just don’t get why it does that.
Thank you
-Nelis

The problem is probably this:

function Update() {
    if(Input.GetButton("Down") && CanBreak){
       DrillDown();
       CanBreak = false;
       Debug.Log("Pressed Down");
    }
}

it should be

function Update() {
    if(Input.GetButton("Down") && CanBreak){
       CanBreak = false;
       DrillDown();
       Debug.Log("Pressed Down");
    }
}

You simply had it the bool being set to false in the wrong place, meaning it will always be false after the first block is drilled.

Hi here is the script
It casts a ray downwards and checks the tag

Edit Working

    public var CanBreak:boolean = true;
var range:float = 1;
function Update(){
if(Input.GetButton("Down") && CanBreak){
CanBreak = false;
Drill();
}
if(!Input.GetButton("Down")){
CanBreak = true;
}
}
 
function Drill(){
var hit:RaycastHit;
if(Physics.Raycast(transform.position,-Vector3.up,hit,range)){

if(hit.collider.tag == "Dirt"){
Destroy(hit.collider.gameObject);
CanBreak = true;
}
if(hit.collider.tag == "Stone"){
Destroy(hit.collider.gameObject);
CanBreak = true;
}
if(!hit.collider){
CanBreak = true;
}
}
}

Note hit should be typed(Physics.Raycast(transform.position,-Vector3.up,hit <-- tells what type of ray to be casted , if not typed it wont hit any object at all ,which in turn returns null,range))before range else it will only cast a ray and wont know what type of ray

Are you calling DrillDown() somewhere else also? If not then I guess problem is with canBreak = false in the if condition when you are calling in Update function. You assign canBreak = true in DrillDown() and again make it false when the control returns back to the Update function.
Solution : I guess if you make canBreak = false in the DrillDown() and canBreak = true in the Update() outside the if will do the stuff ok. Try it out.