I have been trying to load a couple of AudioClips using the _Resources_
folder. It seemed to be easy, however, it does not work for me properly, I have followed many tutorials and viewed many questions and their solution, but dead-end still.
I am using a Sound Manager Script to play sounds, in which I create references to Audio Clips and initialize them, and an Audio source. Script:
public static AudioClip flashLightSwitchOnSound, flashLightSwitchOffSound;
static AudioSource audioSrc;
// Use this for initialization
void Start ()
{
audioSrc = GetComponent<AudioSource> ();
flashLightSwitchOnSound = Resources.Load<AudioClip> ("sounds/flashlight_sound/large_flashlight_switched_on"); //Error appears here
flashLightSwitchOffSound = Resources.Load<AudioClip> ("sounds/flashlight_sound/large_flashlight_switched_off"); //Error appears here
}
// Update is called once per frame
void Update ()
{
}
public static void PlaySound (string clip)
{
switch (clip) {
case "FlashlightSwitchOn":
audioSrc.PlayOneShot (flashLightSwitchOnSound);
break;
case "FlashlightSwitchOff":
audioSrc.PlayOneShot (flashLightSwitchOffSound);
break;
}
}
That is how I use the above script in the Flashlight script:
if (Input.GetKeyUp(KeyCode.F))
{
FLight.enabled = !FLight.enabled;
if (!isOn)
{
isOn = true;
SoundManagerScript.PlaySound("FlashlightSwitchOn");
}
else
{
isOn = false;
SoundManagerScript.PlaySound("FlashlightSwitchOff");
}
}
When I start the game and start pressing F, it results:
NullReferenceException: Object reference not set to an instance of an object
It appears when loading the audio files in SoundManagerScript. Check the script it's commented.
File Structure: