How to set an object to play animation when clicked on?

For a school project I am trying to figure how to get a tree branch object to play an animation that I’ve made within Unity when the player clicks on it within a certain range. We were given a “GrabObject” script that can move RigidBody objects as if the player is using a “gravity gun.” My tree branches cannot be a RigidBody, but I figure that I can alter this script to detect the branches and have the animation play, but so far everything I’ve tried hasn’t worked. Can anyone give me some advice on this?

using UnityEngine;
using System.Collections;

public class GrabObjects : MonoBehaviour 
{
	public LayerMask layerMask;
	public float grabDistance = 4;
	public float pushForce = 5;

	private Transform cam;
	private Rigidbody obj;
	private RaycastHit hit;
	private CapsuleCollider capsue;
	private bool readyToGrab = false;
	private Vector3 objDesiredPos;
	private Vector3 posOffset;
	private float distance;
	private GameObject joint;
	//private bool holding = false;

	// Use this for initialization
	void Start () 
	{
		cam = GetComponentInChildren<Camera>().transform;
		capsue = GetComponent<CapsuleCollider>();

		Screen.lockCursor = true;
	}

	void Update()
	{
		if(obj == null)
		{
			Physics.Raycast(cam.position, cam.forward,out hit, grabDistance, layerMask);
			if(hit.rigidbody)
			{
				readyToGrab = true;
			}
			else
			{
				readyToGrab = false;
			}
		}

		if(readyToGrab && Input.GetMouseButtonDown(0))
		{
			hit.rigidbody.AddForce(cam.forward * pushForce, ForceMode.Impulse);
		}
	}
	
	// Update is called once per frame
	void FixedUpdate ()
	{
		if(readyToGrab && Input.GetMouseButton(1))
		{
			if(obj == null)
			{
				obj = hit.rigidbody;
				obj.useGravity = false;
				obj.freezeRotation = true;
				//obj.isKinematic = true;
				posOffset = obj.position - hit.point;
				distance = hit.distance;
//						obj.collider.enabled = false;
//						obj.velocity = Vector3.zero;
//						obj.angularVelocity = Vector3.zero;

//				joint = new GameObject("GrabJoint", typeof(Rigidbody));
//				joint.rigidbody.isKinematic = true;
//				joint.rigidbody.position = obj.position;

//				FixedJoint sj = joint.AddComponent<FixedJoint>();
//				sj.connectedBody = obj;
//				sj.autoConfigureConnectedAnchor = true;
//				sj.spring = 100;
//				sj.maxDistance = 0.1f;
//				sj.damper = 10;
			}
			else
			{
				//objDesiredPos = cam.position + (cam.forward * posOffset.z) + (cam.right * posOffset.x) + (cam.up * posOffset.y);
//				RaycastHit[] hits = Physics.RaycastAll(cam.position, cam.forward, grabDistance + obj.collider.bounds., layerMask);
//				for(int i = 0; i < hits.Length; i++)
//				{
					if(obj.collider.bounds.Intersects(collider.bounds))
					{
						Debug.Log("Touching Obj");
					}
//				}
//				Debug.Log(obj.collider.bounds.Intersects());
				objDesiredPos = cam.position + (cam.forward * distance) + posOffset;
//				if((objDesiredPos - obj.position).sqrMagnitude > 0.01f)
//				{
					obj.position = Vector3.Lerp(obj.position, objDesiredPos, Time.deltaTime * 10) ;
				//obj.MovePosition(objDesiredPos);
//				}
//				else
//				{
					//obj.position = objDesiredPos;
					//joint.rigidbody.position = objDesiredPos;
//				}
			}
		}
		else
		{
			if(obj)
			{
				//Destroy(joint);
				obj.useGravity = true;
				obj.freezeRotation = false;
				//obj.isKinematic = false;
//				obj.collider.enabled = true;
			}
			obj = null;
		}
	}

	void OnGUI()
	{
		if(readyToGrab)
		{
			GUI.Box(new Rect(Screen.width/2 - 2.5f, Screen.height/2 - 2.5f, 20,20), "O");
		}
		else
		{
			GUI.Box(new Rect(Screen.width/2 - 2.5f, Screen.height/2 - 2.5f, 20,20), "X");
		}
	}

//	void OnDrawGizmos()
//	{
//		if(joint)
//		{
//			Gizmos.color = Color.yellow;
//			Gizmos.DrawWireSphere(joint.rigidbody.position,0.25f);
//		}
//	}
}

It’s a lot of code for a simple question. Anyway, here’s a short answer. Just use Play() function of an object that has an animation component.

myAnimatedObject.animation.Play("My_Animation");

Plus, it is NOT recommended to detect input in FixedUpdate, you might wanna change that to Update. Good luck.

Well, figured out how to go about doing it with some outside help using a bit of JS and attaching the script to the branch.

#pragma strict

var bool = false;

function Start () {

}

function Update () {
if(bool == false && Input.GetMouseButton(1))
{
this.animation.Play("InteractiveBranchAnim");
bool = true;
}

}