hello, I need help allowing users to place Audio files into my game

ok so as the title says Im having issues getting files to work with my game (I have not got a clue on what im doing) I really need some help if anyone has any code or anything that can help it would be much appreciated
(and err is this the right place to be asking for help?)

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;

public class SoundManageSolo : MonoBehaviour
{



    public AudioSource BGM;
    // Start is called before the first frame update
    void Start()
    {
       
    }

    // Update is called once per frame
    void Update()
    {


    }

    public void sound()
    {
        var dir = new DirectoryInfo(Application.dataPath + "/Sounds/Play1");
        var info = dir.GetFiles("*.*");
        var music = info[0];
        BGM.Stop();
        BGM.clip = music;
        BGM.Play();
    }
}

Using “var” for every variable declaration makes it unnecessarily difficult to follow what types you’re using in your code when you post it on the forum. But I’m going to take a wild guess that dir.GetFiles(“.”); doesn’t actually return an array of type AudioSource.clip. So I’m expecting you’re hitting a compile error on line 31.

I’m pretty certain that the audio file asset importer is only available in the editor, and not at runtime. So you’ll either need to get a 3rd party library to import audio assets at runtime, or write your own (I’m not really suggesting writing your own, as that would likely be a pretty advanced level project on its own).

1 Like