Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

[SOLVED ] Raycast door not working?

Discussion in 'Scripting' started by AtomicCabbage33, Jan 23, 2016.

  1. AtomicCabbage33

    AtomicCabbage33

    Joined:
    Aug 31, 2015
    Posts:
    141
    My raycast door script isn't working. I get the error 'object reference not set to an instance of object' when I walk into it which is a bit weird. Everything is assigned in the inspector so
    Code (JavaScript):
    1. private var guiShow : boolean = false;
    2. private var isOpen : boolean = false;
    3.  
    4. var door : GameObject;
    5.  
    6. var rayLength = 10;
    7.  
    8. function Update()
    9. {
    10.     var hit : RaycastHit;
    11.     var fwd = transform.TransformDirection(Vector3.forward);
    12.  
    13.     if(Physics.Raycast(transform.position, fwd, hit, rayLength))
    14.     {
    15.         if(hit.collider.GameObjects.tag == "Door")
    16.         {
    17.             guiShow = true;
    18.             if(Input.GetKeyDown("e") && isOpen == false)
    19.             {
    20.                 door.GetComponent.<Animation>().Play("Open");
    21.                 isOpen = true;
    22.                 guiShow = false;
    23.             }
    24.             else if(Input.GetKeyDown("e") && isOpen == true)
    25.             {
    26.                 door.GetComponent.<Animation>().Play("Close");
    27.                 isOpen = false;
    28.                 guiShow = false;
    29.             }
    30.         }
    31.     }
    32.     else
    33.     {
    34.         guiShow = false;
    35.     }
    36. }
    37.  
    38. function OnGUI()
    39. {
    40.     if(guiShow == true && isOpen == false)
    41.     {
    42.         GUI.Box(Rect(Screen.width / 2, Screen.height / 2, 100, 25), "Use Door");
    43.     }
    44. }
    I'm confused.
     
  2. mickmarona

    mickmarona

    Joined:
    Apr 7, 2015
    Posts:
    44
    On what line are you getting the null reference exception?
     
  3. AtomicCabbage33

    AtomicCabbage33

    Joined:
    Aug 31, 2015
    Posts:
    141
    It doesn't say.. I just get 'Boo.Lang.Runtime.RuntimeServices.GetDispatcher (System.Object target, System.String cacheKeyName, System.Type[] cacheKeyTypes, Boo.Lang.Runtime.DynamicDispatching.DispatcherFactory factory)'
     
  4. Manawydan

    Manawydan

    Joined:
    Nov 25, 2015
    Posts:
    30
    Code (JavaScript):
    1. private var guiShow : boolean = false;
    2. private var isOpen : boolean = false;
    3. private var door : GameObject; // can be private, or you can no use
    4. var rayLength = 10;
    5. function Update()
    6. {
    7.     var hit : RaycastHit;
    8.     var fwd = transform.TransformDirection(Vector3.forward);
    9.     if(Physics.Raycast(transform.position, fwd, hit, rayLength))
    10.     {
    11.         if(hit.collider.GameObjects.tag == "Door")
    12.         {
    13.             door = hit.collider.gameObject; // assing current dor when hit raycast
    14.             guiShow = true;
    15.             if(Input.GetKeyDown("e") && isOpen == false)
    16.             {
    17.                 door.GetComponent.<Animation>().Play("Open");
    18.                 isOpen = true;
    19.                 guiShow = false;
    20.             }
    21.             else if(Input.GetKeyDown("e") && isOpen == true)
    22.             {
    23.                 door.GetComponent.<Animation>().Play("Close");
    24.                 isOpen = false;
    25.                 guiShow = false;
    26.             }
    27.         }
    28.     }
    29.     else
    30.     {
    31.         guiShow = false;
    32.     }
    33. }
    34. function OnGUI()
    35. {
    36.     if(guiShow == true && isOpen == false)
    37.     {
    38.         GUI.Box(Rect(Screen.width / 2, Screen.height / 2, 100, 25), "Use Door");
    39.     }
    40. }
     
  5. AtomicCabbage33

    AtomicCabbage33

    Joined:
    Aug 31, 2015
    Posts:
    141

    still same error?
     
  6. Manawydan

    Manawydan

    Joined:
    Nov 25, 2015
    Posts:
    30
    you have "Door"?
     
  7. AtomicCabbage33

    AtomicCabbage33

    Joined:
    Aug 31, 2015
    Posts:
    141
    I deleted the script and re typed it and now it works. Must have typed something wrong. Thanks for help anyway