Hi guys,

I know this is probably obvious though i cant seem to work it out.

i have a treasure chest with animation on it, when i click on the chest with the mouse, i want unity to play that animation.

This is the code i have though i cant seem to get it working…

using UnityEngine;
using System.Collections;

public class ClickMesh : MonoBehaviour {
private string getName;
	// Use this for initialization
	void Start () {
	
	}
	
void Update(){
   if (Input.GetMouseButtonDown(0)){ // if left button pressed...
     Ray ray = GetComponent<Camera>().ScreenPointToRay(Input.mousePosition);
     RaycastHit hit;
     if (Physics.Raycast(ray, out hit)){

      getName = hit.collider.name;
     	  Debug.Log("Mouse Down Hit the following object: " + hit.collider.name);
     	  getName.GetComponent<Animation>().Play("open");
     }
   }
 }
}


any help would be great :)

The problem is on the getName.GetComponent it expects it to be GameObject to work instead you have declared it as string. All you have to do is change the getName to “private GameObject getName” and then set it with getName = hit.collider.gameObject; . or something like this

void Update(){
    if (Input.GetMouseButtonDown(0)){ // if left button pressed...
      Ray ray = GetComponent<Camera>().ScreenPointToRay(Input.mousePosition);
      RaycastHit hit;
      GameObject go = null;
      if (Physics.Raycast(ray, out hit)){
       getName = hit.collider.name;
       go = hit.collider.gameObject;
            Debug.Log("Mouse Down Hit the following object: " + hit.collider.name);
            //also you gonna need to chech if the object is actually a chest
           if(getName == "chest")
                   go.GetComponent<Animation>().Play("open");
      }