Adding sound to MachineGun

I've read and followed the tutorial for adding sound. This is what my Hierarchy looks like.

Weapons Machinegun Machinegun muzzle_flash sparks

I added a machinegun script to the empty machinegun game object. I have tried adding an audio source to my machinegun prefab, my sparks, and my muzzle_flash. If I add it to my sparks I get sound only if I mark Play On Awake, but then the machinegun sound clip just keeps firing. If I unmark the PLay on Awake, I get no sound. If I add it to muzzle_flash I get the same results.

How do I get the machinegun audio clip to play only when I'm pressing down the (Fire button)mouse button or the ctr button? What is the exact procedure? I'm obviously doing something wrong or I done something wrong. Can you help me. Thanx Troy

I would look into using OnKeyDown to fire Play() and OnKeyUp to fire Stop()

var gunSound : AudioClip;  


Here's what I did and it works. You hierarchy should have an empty Mavhinegun game object with a machinegun script along with the muzzle_flash game object dropped into the variable.(You will have to make sure you drop the muzzle_flash into the empty game object Machinegun script so the muzzle_flash will work) But you don't have the actual machinegun yet so you can't drag anything yet. Just remeber to do it. Now grab your machine gun prefab from the weapons file in your project window. Drag it up and drop it into the empty machinegun to make it the child object of the empty Machinegun game object. The ACTUAL machinegun game object when you created it will have the muzzle_flash attached to it as a child of the machinegun. Now add another machinegunscript to the ACTUAL machinegun game object. Now add an "audio source"(componets/audio/audio source) to the ACTUAL machinegun game object. "UNMARK" the "Play on Awake" button but keep the "LOOP" MARKED> This script will have a muzzle_flash variable slot which you are going to drag and drop your Sparks prefab into.(Remember it's the empty machinegun game object in your Hierarchy that controls the muzzle_flash not the actual machinegun.) Go to MIsc file folder in your Project view and find the sparks prefab. NOW! Add an audio source to the Sparks prefab. And this time you will leave the "Play On Awake" MARKED! MARK the LOOP also! Now drag the Sparks prefab with the audio source into- Guess? This took me awhile to figure out. Drag it into the muzzle_flash variable in the actual machinegun game object script. So what you should have in your Hierarchy is two machinegun scripts and and two audio sources. The two machinegun scripts will each have a muzzle_flash variable. The top will have the muzzle_flash and the lower one will have the Sparks. The two audio sources will have the "Play on Awake" UNMARKED in the ACTUAL machinegun and the MARKED on the muzzle_flash variable(Sparks

how do you do this for the mouse when clicked?