'When I click on an Object with an Audio Source all sounds of the game play at the same time
I'm making a sound game for kids where they have to place notes in the proper line that sounds like the note. So I need the notes and the lines to play a sound when clicked so i made a script that look like this to use in all the game objects that are going to play a sound:
public class Playable : MonoBehaviour
{
private AudioSource audioSource;
private AudioClip sfx;
void Start()
{
audioSource = gameObject.GetComponent<AudioSource>();
sfx = audioSource.clip;
}
void Update()
{
if (Input.GetMouseButtonDown(0))
{
audioSource.PlayOneShot(sfx);
}
}
}
The issue is that when i click on any object all the sounds start playing at the same time and I don't know how to fix it. What can I do?
Solution 1:[1]
FIXED: I changed Input.GetMouse.Down to an OnMouseDown method (gameObject needs to have a collider for it to work).
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | marumyau |