- Home /
Which is faster: AudioClips or GameObjects with Audio attached?
Hello, I've got a gun script, the prefab has not an AudioSource attached, every time I hit "Fire1" it shoots, and instantiate a gameobject with an audio clip attached to it. It works fine. This is the point in question:
var holdSound : GameObject
if (Input.GetButton("Fire1"))
{
//Shoot part...
if (bulletSound)
holdSound = Instantiate(bulletSound, bulletSpawn.transform.position, bulletSpawn.transform.rotation);
}
The question is: It would be more convenient for my code optimization to attach an AudioSource to the gun and play AudioClips directly without Instantiating gameobjects everytime i hit fire?
For example:
if (Input.GetButtonDown("Fire1"))
{
//Shoot Part...
audio.PlayOneShot(bulletSound);
}
What's the difference between both methods?
Thanks.
Answer by Piflik · Oct 28, 2012 at 10:44 AM
Instantiate takes quite a bit of resources, so making the gun play the sound is the better way to do it. Although I would use audio.play instead of PlayOneShot, since the latter is not really better in terms of resources than instantiate, because it is more or less the same thing.
Your answer
Follow this Question
Related Questions
How to call(start play) Sound attached to a gameObject by destroing other gameObject? 1 Answer
audioclip is not working 0 Answers
My AudioClip wont loop even though loop is true 1 Answer
2d PlayAudioOneShot inconsistent behavior 0 Answers
Playing many audioclips with PlayOneShot causes all sound to cut out. 0 Answers