Asset path

Hey, I have a UI Dropdown and in the Start() I am loading a list of audio files that have the .aiff extension. When the player selects an item from the dropdown I am trying to replace an Audio Clip but the path seems to be incorrect?

I am creating a Dictionary and using the Name of the file at the path as the KEY and then saving the path as the value. When the user pulls down the dropdown and selects the item I am using the KEY to get the path to the file but the Audio Clip is still null. I am sure I am screwing up the path some how but when I print the path it seems valid

I am working off of a thumb drive called NO NAME at the moment while out of the office.

    void Start () {
        clipLengthText.text = audioClip.length.ToString();
        nameField.text = audioClip.name;
        DirectoryInfo levelDirectoryPath = new DirectoryInfo(Application.dataPath);
        fileInfo = levelDirectoryPath.GetFiles("*.*", SearchOption.AllDirectories);
        dropNamesList = new List<string>();

        for (int i = 0; i < fileInfo.Length; i++)
        {
            if (fileInfo[i].Name.EndsWith(".aiff"))
            {
                dropNamesList.Add(fileInfo[i].Name);
                print("Info: " + fileInfo[i].Name);
                print(fileInfo[i]);
                pathDict.Add(fileInfo[i].Name, fileInfo[i].ToString());
                //print("fn: " + pathDict[fileInfo[i].FullName]);
            }
        }
        listDropDown.ClearOptions();
        listDropDown.AddOptions(dropNamesList);
    }

    public void DropdownValueChanged(Dropdown change)
{
        audioSource.Stop();
        print(dropNamesList[change.value]);
        nameField.text = dropNamesList[change.value];
        AudioClip clip = Resources.Load(pathDict[dropNamesList[change.value]]) as AudioClip;
        print((clip != null)? "The Clip is valid " : "No Clip");
        audioSource.clip = clip;
        audioSource.Play();
}

Resources.Load loads from the Resources folder, which is under your project’s Assets folder and ends up as part of your build. It doesn’t load files from arbitrary file system paths.

1 Like

Thanks, problem solved. It was not obvious that I needed to create a Resource folder as well, but it is working now.

Yeah the documentation mentions it works from a Resources folder, but assumes you know what that means. You have to dig into other documentation pages for that answer. They definitely could improve the docs as you’re not the first person to have made this exact mistake on here.