Destroying a FixedJoint if it doesn't have a connectedBody

I’ve been looking everywhere for a solution to this and I can’t seem to find one, mabey it’s even impossible but here’s my problem.

In my scene I have a cube which has 6 smaller cubes on each side. when any of the smaller cubes is clicked another cube appears on that side. this way you can build a vehicle, by putting different types of cubes together. when the vehicle is done the player can turn gravity on by pressing space in order for the vehicle to interact with the ground and other objects.

I want the vehicle to fall apart when too much force it applied to it so it is held together by fixed joints which have a limited break force.

My problem is that sometimes (not everytime) when a cube is placed onto the cube I get this error: “Unity Exeption: Transform out of bounds”

as this error appears, another fixed joint is added to the cube but this one doesn’t have a connected body which means that when gravity is activated it prevents the vehicle from falling.

If I could somehow figure out the fixed joints that don’t have a connected body and remove them, my problem would be solved but i don’t know if that’s even possible.

Here is my script:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;

public class GravityManager : MonoBehaviour {

	public bool gravityEnabled = false;

	private GameObject hitObject;


	RaycastHit hit;

	void Start () {
		//Checking if this object has a Rigidbody component
		//If not, one is added automatically
		if (this.gameObject.GetComponent<Rigidbody>() == null)
		{
			this.gameObject.AddComponent<Rigidbody> ();
		} 
	}

	// Update is called once per frame
	void Update () {

		if (this.gameObject.GetComponent<FixedJoint> ()  != null) 
		{
			this.gameObject.GetComponent<FixedJoint> ().enableCollision = true;
			if (this.gameObject.GetComponent<FixedJoint> ().connectedBody == null) 
			{
				Destroy(this.gameObject.GetComponent<FixedJoint> ());
			}
		}
		
		if(Input.GetMouseButtonDown(0))
		{
			Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
			if(Physics.Raycast(ray, out hit))
			{
				hitObject=hit.collider.gameObject;
				if (hit.collider.gameObject.tag == ("SnapPoint")) {
					 	
					hitObject.transform.parent.transform.parent.gameObject.AddComponent<FixedJoint> ().connectedBody = hitObject.transform.GetChild (0).GetComponent<Rigidbody>();
				}
			}
		}
		//Gravity Manager Start
		if (Input.GetKeyDown (KeyCode.Space)) {
			gravityEnabled = !gravityEnabled;
			if (this.gameObject.transform.parent != null)
			{
				this.gameObject.transform.parent = null;
			}
		}

		//Gravity is ENABLED
		if (gravityEnabled == true) {
			this.gameObject.GetComponent<Rigidbody> ().isKinematic = false;
		}


		//Gravity is DISABLED
		if (gravityEnabled == false) {
			this.gameObject.GetComponent<Rigidbody> ().isKinematic = true;
		//Gravity Manager End
		}
	}
}

This is the part that handles the adding of the Joints:

if(Input.GetMouseButtonDown(0))
		{
			Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
			if(Physics.Raycast(ray, out hit))
			{
				hitObject=hit.collider.gameObject;
				if (hit.collider.gameObject.tag == ("SnapPoint")) {
					 	
					hitObject.transform.parent.transform.parent.gameObject.AddComponent<FixedJoint> ().connectedBody = hitObject.transform.GetChild (0).GetComponent<Rigidbody>();
				}
			}
		}

Update: problem resolved itsself, can’t say much about how because i don’t exactly know what I did to fix it.