Currently I have this working by creating an object that, when created, plays a sound, so I just create that object where I want the sound to be played from.
My question is: Is there a simpler way to play a sound at a point without me having to create a lot of different objects?
No, a positional sound requires that you have an object at that position.
but you can avoid the create and destroy through having a pool (:Array) where you push and pop game objects with attached audiosources and when you find out that it is empty, you create a new one (to drop in on the sound end)
saves you the create and destroy which especially on the iphone can be pretty beneficial
This was written in UnityScript and not C#. This language does no longer exist and is no longer supported. However the actual method does still exists, namely “AudioSource.PlayClipAtPoint”. He even linked to the documentation which does have a C# example. This question was about creating an audio source from code. So you have to know where or when to call that method. We can’t tell you when you may want to call it.
I’ve been using AudioSource.PlayClipAtPoint for a while thinking it was helpful and… well, it’s a performance drain if you use it a lot, such as a machine gun.
As you can see, this is a not a good idea. There is no way to not use the static method, it would be awesome if we did have the audioSource.play(position) in there, but it needs to have a created game object at the moment.
Not sure what you mean by that. This method exists just for convenience. Of course when you need to play a lot of audio, you probably want to cache and reuse your audio sources and probably limit the number of sources. This could be done on a per-gun basis (so every gun has a certain amount of audio sources) or globally. Though global pools for things like that, especially when they are limited can easily create issues like the silent BFG (if you prefer watching a video of john romero explaining it, here you go. I can recommend the whole talk if you can spend the hour)
So when using pools you really should think about how many you really need and how to manage them.