Hi,
I have 26 Objects and 26 AudioSources. For each Obj. I added one Audio. So when I want to touch A I want it to play A.wav Audio. But when I touch A it plays all on the same time.
I have a ray and the objects have a boxcollider my script is attached on the objects.
This should be a little game for my son, to lern the ABC. So when he touches a letter then it should play just for ex. “A”.
Thank you for helping.
(sorry for my english)
The problem is that you are not assuring the Raycast is hitting your specific object. If you are going to be using a Raycast, then you would want to restructure you code so that the raycast only happens once (not a Raycast for every object that has an audio source), and the audio is triggered on that specific object. But an easier solution is to use OnMouseDown(). OnMouseDown() does work for touch. In addition, you don’t need the ‘public var audioQuelle : AudioSource;’ since Unity provides you a shortcut to the audio. Assuming the AudioSource is on the object to be clicked, these few lines should do what you are outlining in your question:
#pragma strict
function OnMouseDown() {
audio.Play();
}
Note if for some reason you want a Raycast solution, let me know and I’ll outline the changes you need to make.