Assigning spring joint connected body at runtime

Hey, I’m trying to make my own RigidBody dragging script (C#) and I thought the following code would work but it gives me “Object reference not set to and instance of an object” errors (lots). Any idea what I’m doing wrong?

	public Transform holdPoint;
	public bool somethingHeld = false;
	public RaycastHit moveableObject;

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

				RaycastHit hit;
				if(Physics.Raycast(transform.position, gameObject.transform.forward, out hit, 2))
				if(hit.collider.gameObject.tag == "Moveable" && somethingHeld == false){
					hit.collider.gameObject.rigidbody.isKinematic = true;
					SpringJoint connectedObject = hit.collider.GetComponent<SpringJoint>();

					somethingHeld = true;

					connectedObject.connectedBody = hit.collider.rigidbody;
					if(hit.collider.gameObject.tag == "Moveable" && somethingHeld == true){
						hit.collider.gameObject.rigidbody.isKinematic = false;

						somethingHeld = false;
				if(hit.collider.gameObject.tag == "Moveable"){


The errors appear when I try to drag the rigidbody.
Many thanks.

You can use two ways to do this:

  1. Assign the scripts and all property before the Debug, and make them not enabled. Enable them when you need. We can call this as Static Way.
  2. Create a script which assigns the scripts programmatically, with AddComponent()<>. We can call this as Dynamic Way.

The difference of this two ways is that with the second you can earn time, and you can use one script for many object.

Hope this helps, Enjoy.

Oops, I just realized I was suppose to get the spring component from my holdPoint gameobject {embarrassed} sorry ^^’ thanks anyway