Resources.load returns null instead of AudioClip

Hello there!
Im having a problem with my audioManager class. My problem is that the GetSoundEffect(effectToPlay) returns null for some reason, I have no idea why. The “Sound Emitter” object gets created successfully with an AudioSource component attached.
I have tried to change the “as Audioclip” to a “typeof(AudioClip)” parameter in the return statement of the switch. That led to an error saying:

Error CS0266: Cannot implicitly convert type UnityEngine.Object' to UnityEngine.AudioClip’. An explicit conversion exists (are you missing a cast?)

What am I doing wrong here? Why doesn’t any audio clip get loaded in to the AudioSource?

Any help is appreciated, thank you!

Here is a screenshot

public class audioManager {

        public enum SoundEffects{
                block
        };
 
 
        public static void PlaySound(SoundEffects effectToPlay){  
                //This returns null for some reason
                GetAudioSource().clip = GetSoundEffect(effectToPlay);
                //Nothing gets played
                GetAudioSource().Play();  
        }//end PlaySound()
 
 
        //Create new sound emitter
        static GameObject soundObjectInstance = null;  
        static AudioSource GetAudioSource(){  
                //if it doesnt excist, create it
                if(soundObjectInstance == null){
                        soundObjectInstance = new GameObject("Sound Emitter");  
                        soundObjectInstance.AddComponent<AudioSource>();  
                }  
                return soundObjectInstance.GetComponent<AudioSource>();
        } //end GetAudioSource()
 
 
 
        static AudioClip GetSoundEffect(SoundEffects effectToPlay){
                switch(effectToPlay){
                case SoundEffects.block:
                        return Resources.Load("Sound/Block_Bounce.wav") as AudioClip;
                break;
 
                }
                return null;
        }//end GetSoundEffect()
 
}//end class

You might want to read the docs again because it clearls said you have to omit the extension.

Also in general it’s better to use one of the typed versions to prevent returning the wrong type of asset:

    return Resources.Load<AudioClip>("Sound/Block_Bounce");

or

    return (AudioClip)Resources.Load("Sound/Block_Bounce", typeof(AudioClip));

ps: When you have to use a cast, it’s in general better to use the normal c-style cast instead of the as-cast. An as-cast will return null if the reference can’t be casted into the given type while the c-style cast will throw an exception and tell you what went wrong.

ps: Hm i had this answer written about an hour ago, but hadn’t submitted yet :smiley: I just looked through my open tabs and found it again…