Unstable script

So i made a script that check if an object is adjacent to any object and connect it to the cockpit (robot’s main part) if so. But the problem is that it’s very unstable : sometimes it works, sometimes it doesn’t work…

I can’t really figure out why…

Here’s the script :

public class ObjectPlacement : MonoBehaviour {

PlayerInteractions playerInteractionsScript;

public bool connected = false;
public bool connectedToCockPit = false;
public bool grabbed = false;

public int connectPointsCount;
public Vector3[] connectPointsPos;

public GameObject[] triggersDetectionGO;
public bool[] connectPointsConnected;
public bool[] connectPointsConnectedToCockpit;

public LayerMask layerMask;

public GameObject[] allPartObjects;

void Start () {
	playerInteractionsScript = Camera.main.GetComponent<PlayerInteractions> ();
	connectPointsConnected = new bool[connectPointsCount]; 
	connectPointsConnectedToCockpit = new bool[connectPointsCount];

	allPartObjects = GameObject.FindGameObjectsWithTag ("RobotConstructionPart");
}

void Update () {
	for (int connectPoint = 0; connectPoint < connectPointsPos.Length; connectPoint++) {
		Debug.DrawRay(transform.position + connectPointsPos[connectPoint], connectPointsPos[connectPoint], Color.red, 1);
		RaycastHit hit;

		foreach (GameObject objectPart in allPartObjects) {
			if (objectPart.transform.position == transform.position + (connectPointsPos[connectPoint] * 2)) {
				connectPointsConnected[connectPoint] = true;
				connected = true;
				if (objectPart.transform.name == "Cockpit" || objectPart.transform.GetComponent<ObjectPlacement>().connectedToCockPit) {
					connectPointsConnectedToCockpit[connectPoint] = true;
					connectedToCockPit = true;
				}
			}
			else {
				connectPointsConnected[connectPoint] = false;
				connectPointsConnectedToCockpit[connectPoint] = false;
			}
		}
	}

	if (connectPointsConnected.All(connectPointConnected => !connectPointConnected)) {
		connected = false;
	}
	if (connectPointsConnectedToCockpit.All(connectPointConnectedToCockpit => !connectPointConnectedToCockpit)) {
		connectedToCockPit = false;
	}

	if (transform.name != "Cockpit") {
		if (connected || connectedToCockPit) {
			transform.rigidbody.constraints = RigidbodyConstraints.FreezeAll;
		} else {
			transform.rigidbody.constraints = RigidbodyConstraints.None;
		}
	} else if (transform.name == "Cockpit" && !playerInteractionsScript.hover) {
		if (connected || connectedToCockPit) {
			transform.rigidbody.constraints = RigidbodyConstraints.FreezeAll;
		} else {
			transform.rigidbody.constraints = RigidbodyConstraints.None;
		}
	}

	if (grabbed) {
		for (int connectPoint = 0; connectPoint < triggersDetectionGO.Length; connectPoint++) {
			triggersDetectionGO[connectPoint].SetActive(false);
		}
		rigidbody.useGravity = false;
	} else if (!grabbed) {
		for (int connectPoint = 0; connectPoint < triggersDetectionGO.Length; connectPoint++) {
			triggersDetectionGO[connectPoint].SetActive(true);
		}
		rigidbody.useGravity = true;
	}
}

}

I’ve got an other script that place objects exacly 1 unit apart from the block i’m looking at, here is the line of code that place the object :

grabbed.transform.position = hit.collider.transform.position + (hit.collider.transform.forward / 2) ;

If you need any other precisions tell me. Thanks in advance !

You are doing physics operations inside Update, instead of FixedUpdate, that could make the scene unstable. Check out this answer: