2
\$\begingroup\$

I'm trying to load an audio clip that's in my assets directory and play it. Here is my C# script:

if (Input.GetKeyDown(KeyCode.X))
 {
     audioSource = gameObject.GetComponent<AudioSource>();
     Debug.Log((audioSource == null) ? "Audio source is null" : "Audio Source is not null");
     string path = "Sounds/Aurora/Aurora_IGSSin";
     Debug.Log(path);
     sound = (AudioClip)Resources.Load(path, typeof(AudioClip)) ;
     Debug.Log((sound == null) ? "Sound is null" : "Sound is not null");
 }

//Output:
//Audio source is not null
//Sounds/Aurora/Aurora_IGSSin
//Sound is null

There is an AudioSource component attached to this object, as proved by the first Debug.Log(). Attached is a picture of my folder, proving that the file path is correct. I've seen 90 different posts and tried this 81 different ways, including using the file extension in the path, omitting the directory and only including the file name, omitting (AudioClip) before Resources.Load(), using as AudioClip rather than typeof(AudioClip) and none of it has worked.

Please, someone help me before I pull my hair out over this. I'm incredibly flustered over being stuck on something that should be so simple, and I'd like to get on with my project.

My Project Directory:

My Project Directory

\$\endgroup\$
2
\$\begingroup\$
Resources.Load()

requires that you have a Resources directory as a chils of the Assets directory where you put everything that has to be loaded with that method.

In your case create a Resources directory and then move in your Sounds directory.

EDIT: the you can simplify your call in this way

sound = Resources.Load<AudioClip>(path);
\$\endgroup\$
  • \$\begingroup\$ I have no idea why none of the threads I looked through said anything about a folder I needed to have....Either way, thanks a million. \$\endgroup\$ – Tanner Fix-It Smith Jun 11 '16 at 21:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.