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