Search Unity

Open door using raycast?

Discussion in 'Scripting' started by Quist, Sep 20, 2014.

  1. Quist

    Quist

    Joined:
    Feb 25, 2014
    Posts:
    284
    I got 2 scripts.

    The first one is called "DoorOpen" and is attached to a door.
    The second one is called "OpenObjects" which is connected to my player.

    What i want to happen:
    The script "OpenObjects" should when (drawGUI == true && Input.GetButtonDown("Fire1"))
    tell the script "DoorOpen" script that doorIsClosed = true
    And the "DoorOpen" script should then do the animations.

    But what happens is that my console says "Assets/OpenObjects.js(14,16): BCE0005: Unknown identifier: 'door'."

    Script information:

    DoorOpen:

    Code (JavaScript):
    1. #pragma strict
    2.  
    3. static var doorIsClosed = false;
    4. var theDoor : Transform;
    5.  
    6. function changeDoorState ()
    7. {
    8.     if (doorIsClosed == true)
    9.     {
    10.         theDoor.animation.CrossFade("DoorOpen");
    11.         //theChest.audio.PlayOneShot();
    12.         doorIsClosed = false;
    13.         yield WaitForSeconds(3);
    14.         theDoor.animation.CrossFade("DoorClose");
    15.         //theChest.audio.PlayOneShot();
    16.         doorIsClosed = true;
    17.     }
    18. }
    OpenObjects:

    Code (JavaScript):
    1. #pragma strict
    2.  
    3. var hit : RaycastHit;
    4. var Reach : float = 2.0;
    5. var RayHit : boolean;
    6.  
    7. private var drawGUI = false;
    8.  
    9. private var doorOpen : DoorOpen;
    10.  
    11. function Awake ()
    12. {
    13.     door = GameObject.FindGameObjectWithTag("door");
    14.     doorOpen = door.GetComponent(DoorOpen);
    15. }
    16. function Update ()
    17. {
    18.     var fwd = transform.TransformDirection (Vector3.forward);
    19.     Debug.DrawRay(transform.position, fwd * Reach, Color.red);
    20.     if (Physics.Raycast (transform.position, fwd, hit, Reach) && hit.transform.tag == "door")
    21.     {
    22.         RayHit = true;
    23.         drawGUI = true;
    24.      }
    25.     else
    26.     {
    27.     RayHit = false;
    28.     drawGUI = false;
    29.  
    30.     }
    31.  
    32.     {
    33.     if (drawGUI == true && Input.GetButtonDown("Fire1"))
    34.     {
    35.         doorOpen.doorIsClosed = true;
    36.     }
    37.   }
    38. }
    39.  
    40. function OnGUI ()
    41. {
    42.     if (drawGUI == true)
    43.     {
    44.         GUI.Box (Rect (Screen.width*0.5-51, 200, 102, 22), "Click to open");
    45.     }
    46. }
     
    Last edited: Sep 20, 2014
  2. GBeats

    GBeats

    Joined:
    May 22, 2014
    Posts:
    4
    I dont know javascript but you dont have a declaration for 'door' is that a problem in javascript.

    you should also check that you are in fact getting an object returned on lines 13 and 14.
     
  3. BmxGrilled

    BmxGrilled

    Joined:
    Jan 27, 2014
    Posts:
    239
    change line 13 from;
    door = GameObject.FindGameObjectWithTag("door");
    to
    var door = GameObject.FindGameObjectWithTag("door");

    Hope this helps! :)
     
  4. Quist

    Quist

    Joined:
    Feb 25, 2014
    Posts:
    284
    sadly not :i
     
  5. GBeats

    GBeats

    Joined:
    May 22, 2014
    Posts:
    4
    Is the console giving the same error message?
    Unknown Identifier Error is an error caused by not declaring a Variable before it is used
    like

    a = b or MyObject = GameObject.FindGameObjectWithTag("Tag")

    I read that javascript requires Implicit Variable Declaration which means that it needs you to tell it what all Variables are before you use them.

    var a = b or var MyObject = GameObject.FindGameObjectWithTag("Tag")

    Another thing to note is, i dont know how everything is setup on your side but if you have more than one door and they both have a Tag 'door', expect to reference the wrong door with your current code.

    also

    this is just a suggestion, try and rethink your logic... let me explain. this is not a put down..... an object should be solely responsible for knowing its state at all times. if a door is closed ONLY the door should know its closed. That sounds strange how does a door know its closed.... By doing it that way you can implement and code things in a much simpler way.

    LOGIC FOLLOWING....

    ray hits door
    ray script asks door to activate (or maybe more precisely open or close)
    door checks its current state then either opens or closes and does whatever you need it to do
    door replies "OK" or "I am already open" or whatever you need.

    So in your case

    OpenObjects Script only needs to be concerned with what the ray is hitting and uses the same code to request activation from anything.....GetObject -> RequestActivation

    then

    put a routine on every object that handles this request
    Cup.Request might = drink
    door.Request might = OpenClose
    etc
    etc

    Anyway please check your console and be sure the message is still the same or if its changed
     
  6. Quist

    Quist

    Joined:
    Feb 25, 2014
    Posts:
    284
    so i have the script to only be used on the door, but now when the animation plays it lets the door go through the player, and the player cant normally go through... Its only when the animation plays?