Missing reference to object

How can I make this only run once, because it seems like it searches for the object after it removed it, I’ll show you what I mean.

In script of main object:

FixedUpdate(){
    if(Match){
    			bool doOnce = true;
    			if(doOnce){
    				doOnce = false;
    				Hex hex;
    				hex = transform.Find("Hex").gameObject.transform.GetComponent<Hex>();
    			hex.DetachHex();
    			}
    			transform.localScale = Vector3.Lerp(transform.localScale, new Vector3(0.1f, 0.1f, 0.1f), 1*Time.deltaTime ); 
    		}
}

in hex:

   	public void DetachHex(){
		transform.parent = null;
	}

But when I run the code this happens:

NullReferenceException: Object reference not set to an instance of an object
MainScript.FixedUpdate ()

Do not declare your doOnce variable inside your if statement, rather do it outside of any method. Currently whenever the match condition is true it is setting the doOnce to true and executing whatever is inside the if(doOnce), which means each time on match condition is true.

Change it to:

bool doOnce = true; // Now this is declared outside

FixedUpdate(){
    if(Match){                    
                if(doOnce){
                    doOnce = false;
                    Hex hex;
                    hex = transform.Find("Hex").gameObject.transform.GetComponent<Hex>();
                hex.DetachHex();
                }
                transform.localScale = Vector3.Lerp(transform.localScale, new Vector3(0.1f, 0.1f, 0.1f), 1*Time.deltaTime ); 
            }
}