title: (Unity3d) **nullreferenceexception** thrown when *instance.lockpos()* is called; plus how to make an object maintain its starting rotation
body: <p>I've run up against a little problem, and I can't seem to find the solution.</p>
  <p>I'm putting together a game in Unity3d that includes these gauntlets that can control the environment, i.e. raise downed bridges, open doors, etc. First, I'm just trying to allow the gauntlets to access objects from afar, and I found a telekinesis tutorial online that I am trying to pseudo-repurpose.</p>
  <p>This script is supposed to raise a bridge (hence BridgeMover) until it hits a specific position and then lock it in place. Unfortunately, I'm throwing a null reference exception when the bridge hits the &quot;lockbox&quot; object, which is a small cube on the edge of the bridge ramp leading up to the bridge. I know this is simple, but I can't quite conceptualize it, and I've just been staring at it for a few days. This is my big issue, and any help would be appreciated.</p>
  <p>In other news, I'm also trying to have the bridge rise as a flat plane instead of following the rotation of the cast ray, and would appreciate any advice about that as well, but I would understand if y'all want me to suffer through on that one. I'm pretty sure it has something to do with the Lerp, which I still don't really understand.</p>
  <p>In the hierarchy, I have a handhold object, which is a child of the gauntlet object, which is a child of the camera, which is a child of the player. So</p>
  <pre><code>Player
      Camera
          Gauntlet
              handHold
  </code></pre>
  <p>The lockbox object is a child of the ramp that it's attached to. So:</p>
  <pre><code>Ramp
      Lockbox
  </code></pre>
  <p>Also, this isn't a criticism of the tutorial; I'm just swimming slightly out of my depths here. The tutorial, for those interested, can be found <a href="https://itnext.io/telekinesis-in-unity-part-4-gamedev-series-8cc110b8ab55" rel="nofollow noreferrer">here</a>.</p>
  <pre><code>public class HandController : MonoBehaviour
  {
  
      public static BridgeMover instance;
      public Camera mainCamera;
      Rigidbody rbOfHeldObject;
      GameObject heldObject;
      public GameObject handHold;
      bool holdsObject = false;
      float interactionDistance = 100;
      float slideSpeed = 2;
      Quaternion currentRotation;
  
      private void Start()
      {
          mainCamera.GetComponent&lt;Camera&gt;();
      }
  
      private void Update()
      {
          //HandMove();
          if (Input.GetMouseButtonDown(0) &amp;&amp; !holdsObject)
              Raycast();
          if (Input.GetMouseButtonUp(0) &amp;&amp; holdsObject)
              ReleaseObject();
          if (holdsObject &amp;&amp; instance.LockPos())
              LockObject();
          if(holdsObject)
          {
              if (CheckDistance() &gt;= 0.1f)
                  MoveObjectToPosition();
          }
      }
  
      private void Raycast()
      {
  
          Ray ray = mainCamera.ViewportPointToRay(new Vector3(0.5f, 0.5f, 0));
          RaycastHit hit;
          if (Physics.Raycast(ray, out hit, interactionDistance))
          {
              if(hit.collider.CompareTag(&quot;Bridge&quot;))
              {
                  heldObject = hit.collider.gameObject;
                  heldObject.transform.SetParent(handHold.transform);
  
                  holdsObject = true;
  
                  rbOfHeldObject = heldObject.GetComponent&lt;Rigidbody&gt;();
                  rbOfHeldObject.constraints = RigidbodyConstraints.FreezeAll;
              }
          }
      }
  
      public float CheckDistance()
      {
          return Vector3.Distance(heldObject.transform.position, handHold.transform.position);
      }
  
      private void MoveObjectToPosition()
      {
          currentRotation = heldObject.transform.rotation;
          heldObject.transform.position = Vector3.Lerp(heldObject.transform.position, 
              handHold.transform.position, slideSpeed * Time.deltaTime);
          heldObject.transform.rotation = currentRotation;
      }
  
      private void ReleaseObject()
      {
          rbOfHeldObject.constraints = RigidbodyConstraints.None;
          heldObject.transform.parent = null;
          heldObject = null;
          holdsObject = false;
      }
  
      private void LockObject()
      {
          rbOfHeldObject.useGravity = false;
          heldObject.transform.parent = null;
          heldObject = null;
          holdsObject = false;
      }
  }
  </code></pre>
  <p>And then in the BridgeMover class:</p>
  <pre><code>public class BridgeMover : MonoBehaviour
  {
      public static BridgeMover instance;
      bool lockPos = false;
  
      private void OnCollisionEnter(Collision collision)
      {
          if (collision.gameObject.tag == &quot;LockBox&quot;)
          {
              lockPos = true;
          }
      }
  
      public bool LockPos()
      {
          return lockPos;
      }
  }
  </code></pre>
  <p>Any help would be appreciated. Also, if any further information is needed, let me know and I'll add it.</p>