I’m trying to create a script where if I click on a certain game object, it plays an audio source. So far I got it to play the audio source whenever I click the mouse button but I only want it to play if I click a certain game object.
Depends what platform you are planning to use. In both situations you have to throw a collider on the GameObject.
If you aren’t building to mobile, only platforms that use mouse clicks – you could just throw a script that has an OnMouseDown function on it that calls audio.Play();
For a more general approach, you want to use Raycasting. Here is an example:
if ( Input.GetMouseButtonDown(0)){
var hit : RaycastHit;
var ray : Ray = Camera.main.ScreenPointToRay (Input.mousePosition);
if (Physics.Raycast (ray, hit, 100.0))
if(hit.collider.transform == target)
target.audio.Play();
}
Add collider component to object and reference the audio.
void OnMouseDown()
{
sound.Play();
}