How do i add sound to this script ?

var bulletCasePrefab : GameObject;
var rate : float = 8.0;
var velocity : Vector3;
var on : boolean = true;

private var nextbulletCaseTime : float;

function Update () {
	if (on){
		if(Time.time > nextbulletCaseTime){
			nextbulletCaseTime = Time.time + (1.0 / rate);
			var newBulletCase : GameObject = Instantiate(bulletCasePrefab,transform.position,transform.rotation);
			newBulletCase.GetComponent(bulletCase).velocity = transform.TransformDirection(velocity) + Random.insideUnitSphere * 0.3;
		}
	}
}

You need to supply more information than that. What you are trying to do is probably very simple and can be solved quickly, but this question is going to get ignored because it's not complete. First of all, what's the code that you've posted do, and what are you trying to make happen?

–

2 Answers

2

Add the “Audio Source” Component to your object.

var shootSound :AudioClip; // be sure to add your audio file to this variable in the inspector

// if you want to play an audio file

audio.clip = shootSound;
audio.Play();

You want to add sound to your gun?

First off, add an Audio Source to the object that this script is attached to (Component > Audio > Audio Source).

Secondly, drag the audio file you want to play into the Audio Source you just added.

Now, change your script accordingly:

var bulletCasePrefab : GameObject;
var rate : float = 8.0;
var velocity : Vector3;
var on : boolean = true;

var audioFired : boolean = false;
 
private var nextbulletCaseTime : float;
 
function Update () {
    if (on){
       if(Time.time > nextbulletCaseTime){

       	 //Call our audio function
       	 if(audioFired == false){
       	 	PlayAudioFire();
       	 	audioFired = true;
       	 }

         nextbulletCaseTime = Time.time + (1.0 / rate);
         var newBulletCase : GameObject = Instantiate(bulletCasePrefab,transform.position,transform.rotation);
         newBulletCase.GetComponent(bulletCase).velocity = transform.TransformDirection(velocity) + Random.insideUnitSphere * 0.3;
       }
    }
}


funciton PlayAudioFire(){
	audio.Play();
	audioFired = false;
}

It doesn't work it says Assets/Soldier/Scripts/guns/bulletCaseGenerator.js(6,33): UCE0001: ';' expected. Insert a semicolon at the end.

–