@TriggerScript
Check out the following
public GameObject object1;
public Animator ObjectAnimator;
public Animation ObjectAnimation;
public AudioClip ObjectAudio;
private AudioSource audioSource;
private Animation animation;
void Start ()
{
// make sure that we have an AudioSource - do this here once instead of every frame
if (audioSource == null)
{ // if AudioSource is missing
Debug.LogWarning("AudioSource component missing from this gameobject. Adding one.");
// let's just add the AudioSource component dynamically
audioSource = gameObject.AddComponent<AudioSource>();
}
// make sure that object1 is a valid GameObject and has an animation - do this here once instead of every frame
if ((object1 != null) && (object1.GetComponent<Animation>() != null))
{
animation = object1.GetComponent<Animation>();
}
}
void Update ()
{
if ((Input.GetMouseButtonDown (0)) && (object1 != null))
{
animation.Play();
audioSource.PlayOneShot(coinSFX);
}
}