I must be missing something here, but everywhere I look I can’t find what I’m sure must exist - ie a really simple way to play a sound via code. Here’s what I’m trying to achieve:
I have a bunch of sounds I would like to randomly play - background sounds, not linked to individual specific game objects. Think of a park scene for example, where you might have a bird sound, a lawnmower in the distance, some voices calling out etc. All called randomly so they aren’t the same every time you run the scene.
I have imported all these sounds into my resources folder.
I want to load one of those sounds and then play it, using C#.
The only way I’ve managed to achieve this so far is to create a prefab game object for every single one of my sound effects, with an AudioSource component attached, with the related sound set as its audio clip and then instantiate this prefab and play its associated sound, but surely there must be a simpler way?
I’m hoping there is either a way to have one prefab that can dynamically call a different wav files OR just skip the prefab altogether and create a game object dynamically with code, attach an AudioSource component to it and then assign a wav file to it… but I can’t figure out how.
Any help massively appreciated - none of the examples I can find dynamically load a wav file from the resources folder in the way I do when assigning a sprite for example to an image component.
These are mostly just very short (<2s) wav files.
Thanks!