- Home /
how do you attach a sound to a gameobject ?
i have a rocket sound and when i fire the rocket i want the sound to be attched to the rocket ,
i have been normally using this
AudioSource.PlayClipAtPoint(m_creationAudioClip, transform.position);
but that obvoiuly does not work for this , so i was wondering how its done
Answer by DaveA · Aug 01, 2012 at 11:28 PM
Just select the object, go to the Component menu, Audio, AudioSource
Answer by rutter · Aug 01, 2012 at 11:29 PM
If you don't already have an AudioSource component attached to your rocket object, now is probably a good time to add one:
Are you creating the rocket by instantiating a prefab, perhaps? If so, then your solution can be very simple: an AudioSource can be set to begin playing its default clip as soon as it's spawned.
If you need more control, it's still fairly simple. You can get a reference to the rocket object, call GetComponent() to find its AudioSource, and then configure/control the component as needed.
For example, you could set its default clip:
audio.clip = m_creationAudioClip;
Or tell it to play:
audio.Play();
I realize this is a pretty general answer. I can be more specific, if there's some specific part of this that's giving you trouble.
Your answer