"Ran out of virtual channels. Sound will not be played." Get error after adding environment sounds

Hello,

I have a procedural generated world. When a Player gets near a torch, I want to hear the torch.
They are not meant to play when they spawn, but play when player is near.

This is my Code:

    public static void AddAudiosource (Vector3 pos, ushort voxel, Chunk chunk, AudioClip clip, float maxDist = 0)
    {
        try {
            GameObject soundGameObject = new GameObject ("ChunkSoundAudioAmbient" + voxel);
            Instantiate (soundGameObject, chunk.transform);

            if (clip != null) {
                soundGameObject.transform.position = pos + Vector3.up;       
                // Add an AudioSource component and set up some defaults
                AudioSource audioSource = soundGameObject.AddComponent<AudioSource> ();
                audioSource.playOnAwake = true;
                audioSource.rolloffMode = AudioRolloffMode.Logarithmic;
                audioSource.minDistance = 0f;
                audioSource.maxDistance = maxDist;
                audioSource.spatialBlend = 1;
                audioSource.rolloffMode = AudioRolloffMode.Linear;
                audioSource.clip = clip;
                audioSource.loop = true;
                audioSource.Play();
            }
        }
        catch (System.Exception ex) {
            Debug.LogError ("In FktLight AddAudiosource Error: " + ex.ToString ());           
        }
    }

When spawning my landscape now, I get a lot of error Messages:

“Ran out of virtual channels. Sound will not be played.”

What is the best way to add multiple environment sounds which play when near?
How can my Code be improved? Thanks! :slight_smile:

Hello,

I still found no solution for this problem. If I use PlayOneShot it does not work.
If I do not use Play at all (I thought playOnAwake is true and also Loop so it starts from itself when I am near) it also does not work.

Of course it works this way if I have < 20 objects with audiosources or something around this value.

But there must be a (performant) way to have more than 20 static Environment sounds which Play when I am near?

Or do I have to create a list of 20 audiosources and re-arrange them each second with surrounding stuff? That would Sound like a huge Performance Problem for me…

How is the best way to solve this?

Thanks a lot and have a nice Weekend! :slight_smile:

Have you been able to find a solution? I’m wondering right now of how to make a lot of environmental sounds.

The first idea that came to my mind is to have a single audiosource that is positioned on a player gameobject and when you enter the hearable range of the object the audiosource is positioned at the object’s position.

Second idea is to disable unused sources and only enable them when you enter a certain area (a room for example)

I check relevant objects for player distance. If close enough, I add the audiosource.
If too far away, I destroy it again.
That’s not perfect for a large procedural environment, but at least it works.

Make the audiosources in empty prefabs, then child the prefab to your torchs (prefabs also I assume).

Then you can toggle the torch audio prefab component on/off (enable/disable) based on player distance to those torch objects.