OnMouseDown inside if statment

Hi, i have a script to open and close a animated door on mouse click, the script use OnMouseDown Function to play the open animation when you click on the door collider, the problem its when you want to play the close animation clicking on the same (but) opened door, i cant use OnMouseDown Function twice on the same script, i try to create a “if” statment to activate the OnMouseDown Function based on a new variable that changes if the door is opened or if its closed, but i cant put the functions inside “if” statments.

Anyone perhaps have an idea on how to do this?

Thanks!

this is part of the code:

function OnMouseDown () {

doorObject.animation.Play ("DoorOpen");

audio.PlayOneShot(sound1);

print("The door is open!");

}


// this second OnMouseDown Function gives me an error

function OnMouseDown () {

doorObject.animation.Play ("DoorClose");

audio.PlayOneShot(sound2);

print("The door is close!");

} 

I dont use “Input.GetMouseButtonDown (0)”, beacuse i want the animation plays only when you click “over” the door…

You can have only one event handler in your class. Instead you can use a flag to remember the door state:

var doorOpen = false;

function OnMouseDown () {
    if (!doorOpen) {
        // ... open door
    } else {
        // ... close door
    }
    doorOpen = !doorOpen;
}

This is the working code if anybody wants to see it complete, thanks to "gfr" and all the people on the Unity forum for the help!

Note: the 3d animated door has 2 animations, one to open the door and the other to close it, the script above must be attached to the door object with collider and audio source components on it, also you must set on the inspector all the variables on the script (animations and sounds), and set your door object name too.

var animation1 : AnimationClip; //var name for animation 1.
var animation2 : AnimationClip; //var name for animation 2.
var sound1 : AudioClip; //var name for sound 1.
var sound2 : AudioClip; //var name for sound 2.
private var doorObject : GameObject; 
var doorName = "Door00";
var doorOpen = false; 

function Start() {

doorObject = GameObject.Find(doorName); 

}

function OnMouseDown () {

  if (!doorOpen) {

        doorObject.animation.Play ("DoorOpen");

        audio.PlayOneShot(sound1);

        print("Open!");

    } else {

        doorObject.animation.Play ("DoorClose");

        audio.PlayOneShot(sound2);

        print("Close!");

    }

    doorOpen = !doorOpen; 
}