Animation doesn't work

I have a lamp and a character, the lamp starts on and the character in idle, i want to do this: when i click the lamp it turns off and the character starts the “lying down” animation, when i click it again the lamp turns on and the character starts the “waking up” animation. I tried to use RaycastHit and OnMouseOver function, but it doesn’t work.
The following javascript controls the character.

#pragma strict

var anim: Animator;

function Start () {
	anim = GetComponent(Animator);
	anim.Play("Idle",-1,0f);
}

function Update () {
if (lamp.click == true){
	anim.Play("Lying_Down",-1,0f);
}

if(lamp.click == false){
	anim.Play("Waking_Up",-1,0f);
}
}

The following javascript controls the lamp.

#pragma strict

var myLight : Light;
static var click : boolean;

function Start () {
		myLight = GetComponent(Light);
		click = false;
}

function OnMouseOver () {
if(Input.GetButtonDown("Fire1"))
    {
        myLight.enabled = !myLight.enabled; 
		click = true;
}
else{
click = false;
}
}

Make sure your lamp has a collider.

The problem with your code is that you’re checking the Lamp boolean every frame and force-playing the character anim every frame.

Here is how I would do it using Events, the character register to the lamp switch event then get notified when the lamp change state, this way we only play the character animation once for every switch. This is C# but should be easy to do in Javascript.

Lamp class to be attached to the lamp object :
using UnityEngine;
using System.Collections;

public class Lamp : MonoBehaviour
{
    public delegate void LampSwitchEvent(GameObject g);
    public event LampSwitchEvent lampSwitch;

    private Light myLight;

    public bool isLampOn
    {
        get
        {
            return myLight.enabled;
        }
    }
 
    void Start ()
    {
        myLight = GetComponentInChildren<Light>();
    }
 
    void OnMouseDown ()
    {
        myLight.enabled = !myLight.enabled;
        lampSwitch(gameObject);
    }
}

Character class to be attached to your animated character :

using UnityEngine;
using System.Collections;

public class Character : MonoBehaviour
{
    public Lamp myLamp; // to be assigned in unity, drop your lamp object here

    private Animation anim;
 
    private void HandleLampSwitch(GameObject g)
    {
        Lamp lamp = g.GetComponent<Lamp>();

        if (lamp.isLampOn)
        {
            Debug.Log("Lamp is on, waking up");
            anim.Play("Waking_Up");
            anim.PlayQueued("Idle");
        }
        else
        {
            Debug.Log("Lamp is off, Lying down");
            anim.Play("Lying_Down");
        }
    }

    void Start ()
    {
        anim = GetComponent<Animation>();
        anim.Play("Idle");
    }

    void Awake ()
    {
        myLamp.lampSwitch += HandleLampSwitch;
    }

    void Destroy()
    {
        myLamp.lampSwitch -= HandleLampSwitch;
    }
}