- Home /
Play audio clip on camera from another gameobject
Hi, I have two Audio Sources on my camera. One is playing music, and the other one is playing a sound when I click on the surface. I have a script on my terrain object which is checking if I click on the terrain, and if I do it's supposed to play the little "ding" sound. I've positioned the sound on the camera to hear it good, and now I wonder how I can assign that clip to be activated by another gameobject, my terrain. I though I would just have to drag the audio clip to a variable and play by using audio.PlayOneShot(sound1);
, although this didn't work, because it seems like I can't drag an Audio source from one gameobject to another. It's important to hear the sound good, so that's why it has to be 2 sources on the camera.
Can you please tell me what I should do to play ONE audio source on my camera from a script on my terrain?
Thanks!
Answer by fafase · Oct 12, 2013 at 01:52 PM
You could have two ways of doing it.
First you can have a second audio source on your camera. IF you have only one audio source, it should be for the music, if you swap the audio clip, you lose the music.
So you have a second one on your camera but bam, if you use GetComponent you are not sure which it will return.
So to fix this you could have an empty game object (name it ClickOnCam for ex) attached to the camera with the Audiosource. Then your script goes as:
AudioSource audioSource;
public AudioClip clickClip;
void Start(){
audioSource = Camera.main.transform.Find("ClickOnCam").GetComponent<AudioSource>();
audioSource.clip = clickClip;
}
void Update(){
audioSource.Play();
}
the other way is to use
AudioSource.PlayClipAtPoint(clickClip, Camera.main.transform.position);
this will create a new Audio source object that will get destroyed automatically once the clip is done.
And this script should be placed on the original camera (main) or the new camera?
No this is placed on the object that check for the input, you mentioned a click on the terrain. Wherever this is checked, this above is added.
@fafase it doesn't look like it gets the AudioSource component, Debug log says null and it doesn't show up in editor. I placed the ClickOnCam as a child to $$anonymous$$ain Camera and the script was placed in my terrain script CursorGrid2... Any idea?
Now it is, but it's still saying null... Super wierd!
Answer by Gjallanhorn · Oct 12, 2013 at 01:34 PM
You can resolve this easily, in import settings of your audio file, just uncheck "3d sound" and add the audiosource to your terrain.
But, to play another Audiosource in Camera..
AudioSource[] allAudios = Camera.main.gameObject.GetComponents<AudioSource>();
allAudios[1].Play();
allAudios is a array with all audiosources in the camera, so if it doeen't play the right one, just change the index in array... allAudios[0], allAudios[1], allAudios[2] etc..
How do you know the component returned is not the one playing the background music?
Your answer
Follow this Question
Related Questions
audio source not playing the audio clip 2 Answers
Audio sound on gameObject delete? 1 Answer
Using an Audio Listener on an Audio Source 1 Answer
Play audio when gameobject is near 2 Answers
Audio won't play 1 Answer