This should be simple (audio) (solved)

Ok, so I’m working on trying to create a gunshot kind of script. It is to create a prefab that has a few particle effects, and will have a bullet going out of it. I want to make it simple by eventually just dragging on different meshes and sound clips and changing a few variables for things like bullet speed, damage, etc.

Problem is, my instantiate code is duplicating the scripts gameobject rather then just the audio source. All I want to duplicate is the audio source. Any ideas why it’s doing the whole thing?

private var tmr:float = 0; 
var tmrInterval:float = .2; 
var ObjectToGenerate:Transform;

function Update() 
{ 
	if(tmr < tmrInterval) 
	{ 
		var dTime:float = Time.deltaTime;
		tmr += dTime; 
	}else{ 
		// place code here.
		AudioSource.Instantiate(audio);
		transform.Instantiate(ObjectToGenerate, transform.position, Quaternion.identity);
		tmr -= tmrInterval; //The reason we don't = 0 is so it's more accurate over time. 
	} 
}

The reason I want to instantiate is so that it can have overlapping sound. If I just do audio.Play(), it will cut off the last sound and start over. Any help would be nice.

Instead of instantiate, use audio.PlayOneShot. It will, well, make an instance of the sound (called One shot audio, look in the hierarchy). It will make exactly what you are trying to achieve (I think).

That’s exactly what I needed. :slight_smile: Thanks! (I haven’t hardly messed with sound at all.)