How do I stop an 'IF' function from running from another script ?

I need an IF function to NOT get executed altogether, if an object is colliding with another object tagged as ‘Crowd’. How do I go about achieving this. Script A is the one I wish to change. Script B contains the IF function of KeyUp, that I do not wish to execute, if the object, on which Script A is attached to, is colliding with another object tagged Crowd.

Script A (attached to Object A)

    	void OnTriggerEnter2D(Collider2D col)
    	{
    		if (col.gameObject.tag == "Crowd") 
    		{
    //Add code to stop the IF statement from other script			
    Debug.Log ("Collisions are happening");
    		}
    	}

Script B (attached to Object B)

void Update (){

		if(Input.GetKeyDown(KeyCode.Z))
		{
Debug.Log("Key was pressed");
}
		if(Input.GetKey(KeyCode.Z))
		{
Debug.Log("Key is currently pressed");
}
		if(Input.GetKeyUp(KeyCode.Z))     //Needs to stop from getting executed
		{
Debug.Log("Key was stopped from being pressed");     //Needs to stop from getting executed
}      }

Add a Boolean on script B, in the collision method get the component and change the Boolean.

This Boolean is used at the top of the update to return if not the proper condition

Note, that in the end you don’t want to use Find() and GetComponent() all the time like this. These can be cached in your a.Start()

In your “B” script, you want to do something like the following.

public class bBehavior : MonoBehaviour {
	public bool otherIsInCroud = false;

	void Start () {
		otherIsInCroud = false;
	}
	
	void Update () {
		if(Input.GetKeyDown(KeyCode.Z)) { Debug.Log("Z-Key down"); }
		if(Input.GetKey(KeyCode.Z)) { Debug.Log("Z-Key held down"); }
		if(Input.GetKeyUp(KeyCode.Z))
		{
			Debug.Log("Z-Key up");
			
			if ( !otherIsInCroud ){
   				Debug.Log("Object (a) WAS NOT colliding with croud so do something");
			}
			
		}
	}
}

In your “A” script, you want to do something like:

void OnTriggerEnter2D(Collider2D col) {
	if (col.gameObject.tag == "Crowd")  {
		Debug.Log ("Collisions are happening");

		bBehavior b = GameObject.Find ("b").GetComponent<bBehavior> ();
		b.otherIsInCroud = true;
	}
}