help me to get a game object to play a sound on click

I’m super new to Unity and Coding, As practice i built a drum kit and want the drums to play their associated sounds when i click on them.

Im starting off with the kick drum

here is my code:

using UnityEngine;
using System.Collections;

public class playSoundOnClick : MonoBehaviour {

public AudioSource KitSound;

// Use this for initialization
void Start () {

}

// Update is called once per frame
void Update () {
	if(Input.GetMouseButtonDown(0) ){
		KitSound.Play();
	}

}

}
I’ve created the component and added the sound to it. However, the sound plays if I click anywhere in the game not just the game object. Please help!!

@tinytimbre

Input.GetMouseButtonDown(0) will be called anytime you tap on the screen.

try this.

using UnityEngine;
using System.Collections;

public class playSoundOnClick : MonoBehaviour
{

public AudioSource KitSound;
// Use this for initialization
void Start () 
{
	
}

// Update is called once per frame
void Update () 
{

// if(Input.GetMouseButtonDown(0))
// {
// KitSound.Play();
// }

}

void OnMouseDown()
{
	KitSound.Play();
}

}

attach this script to your drum and make sure your drum has collider attached to it.

Thanks…
Now it is working :slight_smile:
I just missed to add collider to my gameobject.
thanks again