Search Unity

How do i access variables on other gameobjects through tags?

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

  1. Quist

    Quist

    Joined:
    Feb 25, 2014
    Posts:
    284
    So i have a player and a lot of doors in my map.
    I want it so when my player is near the door and left clicks on the door it plays the animation DoorOpen, waits 3 seconds, plays the animation DoorClose.

    I have tagged all my doors "door" and given them the script DoorOpen.
    And given my player a script called OpenObjects.

    But when i start my game it doesn´t work "nothing happens".
    Console says: The referenced script on this Behaviour is missing!

    *DoorOpen* script attached to the Doors.

    Code (JavaScript):
    1. #pragma strict
    2.  
    3. static var playerLeftClick = false;
    4. var theDoor : Transform;
    5.  
    6. function changeDoorState ()
    7. {
    8.     if (playerLeftClick == true)
    9.     {
    10.         theDoor.animation.CrossFade("DoorOpen");
    11.         //theChest.audio.PlayOneShot();
    12.         playerLeftClick = false;
    13.         yield WaitForSeconds(3);
    14.         theDoor.animation.CrossFade("DoorClose");
    15.         //theChest.audio.PlayOneShot();
    16.         playerLeftClick = true;
    17.     }
    18. }
    *OpenObjects* attached to the player.
    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. var doorOpen : DoorOpen;
    10.  
    11. function Start()
    12. {
    13.     var door = GameObject.FindWithTag("door");
    14. }
    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.playerLeftClick = 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. }

     
  2. BmxGrilled

    BmxGrilled

    Joined:
    Jan 27, 2014
    Posts:
    239
    You could also use the OnMouseDown() event, inside the door script, then when the user clicks on the door you can have it open/close as necessary. :) Hope this helps!
     
  3. Quist

    Quist

    Joined:
    Feb 25, 2014
    Posts:
    284
    helped a lot, thanks!