Can only get collide trigger to work once

I am very new to programming. I am working on animating a door to open when the trigger is hit, and close when the trigger is exited. Here is my code.

var door : GameObject;

function OnTriggerEnter (collision : Collider) { if (collision.gameObject.tag == "Player") { door.animation.Play ("Bouncing Box"); } }

function OnTriggerExit (collision : Collider) { if (collision.gameObject.tag == "Player") { door.animation["Bouncing Box"].speed = -1.0; } }

Problem is it only works the first time. Is there a reset I have to put somewhere so it works everytime I hit the trigger?

Thank you!

I think you have to put a boolean to say if the door is open ....do that if the door is not open...open it

private var doorIsOpen : boolean = false;
    var door : GameObject;

    function OnTriggerEnter (collision : Collider) 
    { 
       if ((collision.gameObject.tag == "Player") && (doorIsOpen = false))

       {
        door.animation.Play ("Bouncing Box"); 
        doorIsOpen = true;
       }

    }

    function OnTriggerExit (collision : Collider)

     {
     if ((collision.gameObject.tag == "Player") && (doorIsOpen = true))
    {
     door.animation["Bouncing Box"].speed = -1.0;
    doorIsOpen = false; 
    }

I am not a programmer but after finishing a few tutorials I have this working code that works for me. So.. hope it works. With a few alterations it can work onCollider hit too.

If you are a modeller that wants to have a system that works with every door tagged as "doorTrigger", I suggest whenever you import an animated door model, do the following In the 3dPackage A: Create two animations one opens, one closes.(I haven't tried to inverse the animation but it sound a good idea) For example frame 0 to frame 50 opens, 50 to 100 closes.

B: If your trigger has to be very precise in shape (quite rare ) do it in the 3d package and name it trigger something. I usually add a cube as a trigger in the 3d package but you can add them in unity too as a child of the root gameObject.

In unity drag your model in your scene.

A:add the script at the end of the answer in your game First Person Controller or whatever character controller you have. B: When you import the model add the animations in the root Game object. (an option in the import settings). This way you play all your animations for your model by finding your root game object. Use the same name for all your different doors or windows etc( this way whenever you hit a trigger ... called let's say doorOpen you always play an animation called "dooropen" even for different doors or a window etc... C: Give your trigger a tag like "doorTrigger" and check the isTrigger box in the mesh collider. D: (optional)Add sound for the door by adding a sound source to your first person controller or other controller and assign the audio clips under your attached script.

 private var doorIsOpen : boolean = false;
    private var timer : float = 0.0;
    private var doorObject : GameObject;

    var doorOpenSound : AudioClip;
    var doorShutSound : AudioClip;

    function OnTriggerEnter (hit:Collider)
    {
         if((hit.gameObject.tag == "doorTrigger") && (doorIsOpen == false)){
         doorObject=hit.gameObject ;

        doors (doorOpenSound,"dooropen", doorObject,true);

        }

    }

    function Update()
    {

        if(doorIsOpen) 
    //if you activated the trigger you might want the door to close after a while.
  // you can close your door on trigger exit instead ... just make sure that the animation  //is completed before the door closes   
        {
            timer += Time.deltaTime;
        }

        if(timer >= 6)

        {
            doors(doorShutSound, "doorshuts", doorObject, false);       
            timer = 0.0;    
        }

    }

    function doors (aClip: AudioClip ,animName : String, doorObject :GameObject, doorCheck : boolean)
    //here you create a function that combines all of the necessary elements for the //animation
    // your audio clip , the name of the animation you want to play, your gameObject, and  //boolean if the door is open or close.  
    {

    audio.PlayOneShot(aClip);               
    doorObject.transform.parent.animation.Play(animName);
    //this way you play the animation stored in the root object of your model
    doorIsOpen = doorCheck;  

    }
    @script RequireComponent(AudioSource)

To be honest, Animations aren't suited for such simple tasks as opening and closing doors. You may find it easier to just script the door, using Coroutines or something similar. Just "Transform" the door upwards and downwards when the player enters and exits the trigger.

I can't provide more detailed help, because it sounds like it's a problem with the way you've animated the door, and I can't see your animation data.

Hi, I have a similar problem. I made an animation in 3dsmax and put the settings open door frame 30 to 0. When I try to activate the reverse mode(speed = -1)it will not start. Is there any way to make the model back to the starting position, as if the animation did not occur. I tried using rewind but did not work. I just want to close the door after opening animation