problem with cancelling on goal

when the ball collides with the Goal collider it should cancel the DisplayNoGoal()

function OnTriggerEnter(hit : Collider){
    if(hit.gameObject.tag == "Ball")
    {
    	
    		CancelInvoke("DisplayNoGoal");// this function is called from here when person does in fact get a goal..to cancel the DisplayNoGoal()
    }
}
            
   // when the ball is kicked the Invoke function executes as following.
     		if(countBalls.ballcount==1){
				
				Invoke( "DisplayNoGoal", 3.0);
    		}


function DisplayNoGoal()
{
	print("goal missed");
	goalMissed.enabled = true;
}
the problem here is when the person kicks the ball the invoke function executes and delay for 3 seconds but even if  the goal is done by the person the invoke function is not getting canceled...

It sounds like the problem is this block

if(countBalls.ballcount==1) {    
    Invoke( "DisplayNoGoal", 3.0);
}

Since your if statement only checks if ballcount is equal to 1, then every time OnGUI gets called (which is fairly often) and there’s a ball on the court, it’ll re-invoke “DisplayNoGoal”. It’s hard to give a solution without looking at the rest of your code, but there are two things which should work. Either add a boolean flag when you call CancelInvoke(“DisplayNoGoal”), and use that refer to that flag in your if statement like this:

var was_cancelled : boolean = false;

function OnTriggerEnter(hit : Collider){
    if(hit.gameObject.tag == "Ball")
    {

           CancelInvoke("DisplayNoGoal");
           was_cancelled = true;
    }
}

...

if (countBalls.ballcount == 1 && !was_cancelled) {
  Invoke("DisplayNoGoal", 3.0);
}

Or, a simpler solution would be to put your Invoke(“DisplayNoGoal”, 3.0) statement in the same place that you detect throw input. So something like:

function Update() {
    if (Input.GetButton("Throw")) {
        throw_ball();
        Invoke("DisplayNoGoal", 3.0);
    }
}