# Audio script

**URL:** https://discussions.unity.com/t/audio-script/449271
**Category:** Audio & Video
**Tags:** Scripting
**Created:** [July 4, 2011, 5:16pm UTC](https://discussions.unity.com/t/audio-script/449271 "2011-07-04T17:16:44Z")
**Posts on this page:** 14
**Page:** 1

<div class="post-metadata">

### Author: ![Hobbit9797](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/hobbit9797/32/415908_2.png) [@Hobbit9797](https://discussions.unity.com/u/Hobbit9797)
#### Post date: [July 4, 2011, 5:16pm UTC](https://discussions.unity.com/t/audio-script/449271/1 "2011-07-04T17:16:44Z")

</div>

hi  
how do i play a audio play in a script?  
i wanna play footsteps…  
my script:

```auto
var Footstep : audio.clip

function Update () {

if GetKey("w")
{
audio.Play(Footstep);
}
}

```

what is wrong?

---

<div class="post-metadata">

### Author: ![kodagames](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/kodagames/32/86078_2.png) [@kodagames](https://discussions.unity.com/u/kodagames)
#### Post date: [July 4, 2011, 6:25pm UTC](https://discussions.unity.com/t/audio-script/449271/2 "2011-07-04T18:25:11Z")

</div>

is it a FPS character or 3rd person character?

If its a 3rdPerson you could add a collider to the foot and OnCollisionEnter play the audio clip As for FPS Im not sure. If you google it Im sure you’ll find everything you need 🙂 but here is a script or scripts that will allow you to have different sounds depending on what collider your walking on(metal surface, wood surface).

Foot.Js this goes on the actual foot create empty game object called Footsteps One for each foot with colliders etc… Images attached

```auto
var baseFootAudioVolume = 1.0; 
var soundEffectPitchRandomness = 0.05; 

function OnTriggerEnter (other : Collider) 
{ 
 	var collisionSoundEffect : CollisionSoundEffect = other.GetComponent(CollisionSoundEffect); 
 	
 	if (collisionSoundEffect) 
 	{ 
  		audio.clip = collisionSoundEffect.audioClip; 
  		audio.volume = collisionSoundEffect.volumeModifier * baseFootAudioVolume; 
 		audio.pitch = Random.Range(1.0 - soundEffectPitchRandomness, 1.0 + soundEffectPitchRandomness); 
  		audio.Play();  
 	} 
}

function Reset() 
{ 
 	rigidbody.isKinematic = true; 
 	collider.isTrigger = true; 
}

@script RequireComponent(AudioSource, SphereCollider, Rigidbody)

```

CollsionSound.js Then create a floor collider and attach this script

```auto
// Small script to hold a reference to an audioclip to play when the player hits me.

// This script is attached to game object making up your level. 
// The "Foot" script (which is attached to the player) looks for this script on whatever it touches.
// If it finds it, then it will play the sound when the foot comes in contact

var audioClip : AudioClip;
var volumeModifier = 1.0;

```

Now when you walk on the floor it will have a sound effect you can create a different floor with a different sound as well 😉 Hope it helps

 ![621160--22115--$Picture 15.png](https://europe1.discourse-cdn.com/unity/original/3X/6/5/653c9c43dcf181711290cb4fedb60dbeed4b1ca4.png)  
 ![621160--22116--$Picture 16.png](https://europe1.discourse-cdn.com/unity/original/4X/5/f/b/5fb2e8f47097029ca02fb6a19046457a1c63d21e.png)

---

<div class="post-metadata">

### Author: ![Hobbit9797](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/hobbit9797/32/415908_2.png) [@Hobbit9797](https://discussions.unity.com/u/Hobbit9797)
#### Post date: [July 4, 2011, 6:40pm UTC](https://discussions.unity.com/t/audio-script/449271/3 "2011-07-04T18:40:59Z")

</div>

It is in first person perspective

---

<div class="post-metadata">

### Author: ![pixellegolas](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/pixellegolas/32/443111_2.png) [@pixellegolas](https://discussions.unity.com/u/pixellegolas)
#### Post date: [July 4, 2011, 6:54pm UTC](https://discussions.unity.com/t/audio-script/449271/4 "2011-07-04T18:54:46Z")

</div>

have you dragged the audio to the newly created slot that was made when you made the script. It has to be linked to a audio file too

---

<div class="post-metadata">

### Author: ![Hobbit9797](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/hobbit9797/32/415908_2.png) [@Hobbit9797](https://discussions.unity.com/u/Hobbit9797)
#### Post date: [July 4, 2011, 7:08pm UTC](https://discussions.unity.com/t/audio-script/449271/5 "2011-07-04T19:08:15Z")

</div>

there is no created slot…

---

<div class="post-metadata">

### Author: ![Hobbit9797](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/hobbit9797/32/415908_2.png) [@Hobbit9797](https://discussions.unity.com/u/Hobbit9797)
#### Post date: [July 4, 2011, 7:10pm UTC](https://discussions.unity.com/t/audio-script/449271/6 "2011-07-04T19:10:24Z")

</div>

there is a compiling error…

---

<div class="post-metadata">

### Author: ![carking1996](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/carking1996/32/248509_2.png) [@carking1996](https://discussions.unity.com/u/carking1996)
#### Post date: [July 4, 2011, 7:23pm UTC](https://discussions.unity.com/t/audio-script/449271/7 "2011-07-04T19:23:13Z")

</div>

```auto
var Footstep : AudioClip;

function Update () {

if GetKey("w") {

audio.PlayOneShot(Footstep);

}
}

```

---

<div class="post-metadata">

### Author: ![Hobbit9797](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/hobbit9797/32/415908_2.png) [@Hobbit9797](https://discussions.unity.com/u/Hobbit9797)
#### Post date: [July 4, 2011, 7:32pm UTC](https://discussions.unity.com/t/audio-script/449271/8 "2011-07-04T19:32:32Z")

</div>

> [@carking1996](#):
>
> ```auto
> var Footstep : AudioClip;
> 
> function Update () {
> 
> if GetKey("w") {
> 
> audio.PlayOneShot(Footstep);
> 
> }
> }
> 
> ```

it doesnt works:(

---

<div class="post-metadata">

### Author: ![carking1996](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/carking1996/32/248509_2.png) [@carking1996](https://discussions.unity.com/u/carking1996)
#### Post date: [July 4, 2011, 8:01pm UTC](https://discussions.unity.com/t/audio-script/449271/9 "2011-07-04T20:01:42Z")

</div>

```auto
var Footstep : AudioClip;

function Update () {

if (GetKeyDown("w")) {

audio.PlayOneShot(Footstep);

}
}

```

Try that? 😉

---

<div class="post-metadata">

### Author: ![pixellegolas](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/pixellegolas/32/443111_2.png) [@pixellegolas](https://discussions.unity.com/u/pixellegolas)
#### Post date: [July 4, 2011, 9:18pm UTC](https://discussions.unity.com/t/audio-script/449271/10 "2011-07-04T21:18:27Z")

</div>

you have to attach the script to something. Create an empty and attach the script to it… When you click the empty you will see a new slot called somehing like “audio” and just drag the clip there

---

<div class="post-metadata">

### Author: ![Hobbit9797](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/hobbit9797/32/415908_2.png) [@Hobbit9797](https://discussions.unity.com/u/Hobbit9797)
#### Post date: [July 5, 2011, 4:49am UTC](https://discussions.unity.com/t/audio-script/449271/11 "2011-07-05T04:49:33Z")

</div>

> [@carking1996](#):
>
> ```auto
> var Footstep : AudioClip;
> 
> function Update () {
> 
> if (GetKeyDown("w")) {
> 
> audio.PlayOneShot(Footstep);
> 
> }
> }
> 
> ```
> 
> Try that? 😉

it doesnt works…  
the console is saying:“BCE0005: Unknown identifier: ‘GetKeyDown’.”

---

<div class="post-metadata">

### Author: ![DanielQuick](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/danielquick/32/223057_2.png) [@DanielQuick](https://discussions.unity.com/u/DanielQuick)
#### Post date: [July 5, 2011, 4:51am UTC](https://discussions.unity.com/t/audio-script/449271/12 "2011-07-05T04:51:24Z")

</div>

It should be Input.GetKeyDown

---

<div class="post-metadata">

### Author: ![Hobbit9797](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/hobbit9797/32/415908_2.png) [@Hobbit9797](https://discussions.unity.com/u/Hobbit9797)
#### Post date: [July 5, 2011, 5:07am UTC](https://discussions.unity.com/t/audio-script/449271/13 "2011-07-05T05:07:24Z")

</div>

You are my hero!  
thats so typically for me that i forgot a simple Input. ☹  
by the way: it works

---

<div class="post-metadata">

### Author: ![carking1996](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/carking1996/32/248509_2.png) [@carking1996](https://discussions.unity.com/u/carking1996)
#### Post date: [July 5, 2011, 11:36am UTC](https://discussions.unity.com/t/audio-script/449271/14 "2011-07-05T11:36:59Z")

</div>

oh. Forgot that. XD My mistake. 😛
