On level up play audio

I’m trying to make it so that when my player reaches a new level, a sound effect will play representing the level up.

public AudioClip levelUpSound; // my public audioclip used for the code below

if (xp >= miningLevel * multiplier + baseXp)//this is basic formula which you can later edit to suit how fast player should advance
      
{
            miningLevel++;
            print("Congratulations You've Reached Level" + ":" + " " + miningLevel); //On level up print new level
            xp = 0; // hard reset to 0
            NewFeature(); //On certain levels up include new feature like new ore to mine
            GetComponent<AudioSource>().Play();
        }

This is what I’ve got right now and in the inspector I have dragged the audio clip into the correct slot. It just doesn’t play when the player levels up. Any ideas?

I assume the rest of your code works and the if statement is within the Update() method. Anyway, the AudioSource has its own property field where you can assign a clip, which you can use if it’s only meant to play a single sound. If you still want to rely on your own AudioClip reference (levelUpSound), did you remember to assign the it to the AudioSource somewhere in your code? Otherwise the AudioSource won’t know that’s the clip it should play. Other reasons could be the distance between the AudioSource and AudioListener (make sure spatial blend is set to 2D), or that the AudioSource’s volume or pitch is zero.

1 Like

Try all of the above. Is an error being shown in the console/logging?

Is the AudioSource attached to the same GameObject as the script?