AudioSource Not Playing

Hey there! This is a simple problem to understand, so don’t be intimidated by all the text below; it’s just me going deep into detail to avoid having to answer any extra questions. Thank you!

I have two tilemap GameObjects, and one is called “Torches”, and one is called “Waves”. They each consist of tiles the player must be within distance of in order for an ambient sound to play when they closen. These are stored in a Class and organized through AmbienceList, found in dataClasses.

The script works perfectly fine, except at the last part, where it says ambienceSource.Play(); (“Source” is short for AudioSource), nothing happens, and “PLAY AUDIO Waves TRUE” continues to be printed, despite it only printing when the ambienceSource isn’t playing, meaning that the ambienceSource is never actually being played. I get no errors, and I’m just confused as to why the ambienceSource isn’t playing, despite the debugger telling me that the .Play() function is called.

for (int i = 0; i < dataClasses.AmbienceList.Count; i++)//FOR EACH AMBIENCE LAYER
{
   float ambienceDistance = dataClasses.AmbienceList[i].ambienceDistance;
   AudioSource ambienceSource = dataClasses.AmbienceList[i].ambienceSource;

   GameObject ambienceLayer = dataClasses.AmbienceList[i].ambienceLayer;
   Tilemap tileMap = ambienceLayer.GetComponent<Tilemap>();

   bool playAudio = false;
   bool breaking = false;

    for (int x = tileMap.cellBounds.xMin; x < tileMap.cellBounds.xMax; x++)
    {
        if(breaking)
        {
            break;
        }

        for (int y = tileMap.cellBounds.yMin; y < tileMap.cellBounds.yMax; y++)
        {
            Vector3Int localPos = (new Vector3Int(x, y, (int)tileMap.transform.position.y));
            Vector3 pos = tileMap.CellToWorld(localPos);

            if (tileMap.HasTile(localPos))
            {
                if (Vector3.Distance(pos, playerTF.position) <= ambienceDistance)
                {
                    playAudio = true;

                    breaking = true;
                    break;
                }
            }
        }
    }

    if(playAudio && !ambienceSource.isPlaying)
    {
        Debug.Log("PLAY AUDIO " + dataClasses.AmbienceList[i].ambienceName + " TRUE");
        ambienceSource.Play();
    }
         
    if(!playAudio && ambienceSource.isPlaying)
    {
        Debug.Log("stopping " + dataClasses.AmbienceList[i].ambienceName);
        ambienceSource.Stop();
    }
}

Any help would be appreciated. I’ll attach an image. Thanks in advance!


Just bumping this post. Again, any help is appreciated. Thanks!

Still having this issue, does anyone know any reason .Play() wouldn’t work?

Look at your image DebugShot2.PNG. It has the explanation.

You have the same AudioSource for both Waves and Tourches. In your console you hit Play() on Waves, then stop on Torches, which is the same AudioSource. So now when you check Waves again, it is of course already stopped, since you stopped it :stuck_out_tongue:

I’d assume you intended to have different AudioSource components for both Waves and Torches. That will probably fix the problem.

2 Likes

I cannot believe I missed something that obvious!! All that code, and it was just an inspector error. Thanks so much, you’re a life saver :slight_smile:

1 Like