- Home /
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
When you specify the type as a parameter, you still have to cast. You also don't specify the extension.
return Resources.Load("Sound/Block_Bounce", typeof(AudioClip)) as AudioClip;
And are you making sure that your path is relative to a parent folder named "Resources"? From the documentation:
Returns the asset at path if it can be found otherwise returns null. Only objects of type will be returned if this parameter is supplied. The path is relative to any Resources folder inside the Assets folder of your project, extensions must be omitted.
Answer by Bunny83 · Aug 05, 2014 at 04:50 PM
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 :D I just looked through my open tabs and found it again...
Yes this worked perfectly! It must have been a cast error. I will read the documentation more carefully next time. Thank you so much!
I have this problem as well, but even with your method, it still refuses to load the audioclip and keeps giving out null.
Your answer
Follow this Question
Related Questions
Resources.load returns null 1 Answer
Is it possible to load external audio files during a game? 0 Answers
Dynamically load AudioClip during runtime 1 Answer
Resources.Load wont read text file 6 Answers
Play sound at scene change time 1 Answer