Set or load the Audioclip of an audiosource from a folder [C#]

Hello everyone!

I’m a beginner with c#. I want to change the audioclip of an audiosource with a simple click on a button.
The text component of the button contains the name of the clip.
Here’s what I tried :

public class MusicSelection : MonoBehaviour
{
    MusicListManager musicList_Manager;  // contains the method to instantiate a button with the name of the song for each files contained in the corresponding folder
    sliderManager sliderManagerScript;  // I use this script to get my audiosource

    public void selectMusic()
    {
        string clipName;
        clipName = transform.GetChild(0).GetComponent<Text>().text; // thanks to this line I get the name of the audioclip I want to play by clicking on the button
        sliderManagerScript.song = Resources.Load<AudioClip>(clipName); // "song" refers to the clip of my audiosource
    }
}

The console tells me “NullReferenceException : Object reference not set to an instance of an object”
The last line seems to be responsible for that. I really don’t know what’s wrong with this.

Can anyone help me please?

First off are your songs in a resources folder? Second, within that resources folder, is the song in a particular folder that perhaps isn’t part of the clipName variable you’re getting from the button text? Such as “Songs/My Song Name”? If so you would want to use something like (“Songs/” + clipName). Also is the extension of the song part of the name, such as “My Song Name.mp3”? It will not work with an extension.

2 Likes

Thanks for your answer QuinnWinters :slight_smile: The songs are in a resource folder and there is no other folder inside it.
I checked about the “.mp3” extension thing and that is exactly the problem I guess.
I use the following code to have the name of the song as the text component of my button :

buttonToAdd.transform.GetComponentInChildren<Text>().text = file.Name;

By doing that the text is the name of the file and a “.mp3” is added to it.
Of course the file in the resource folder has no “.mp3” in his title.
How can I get rid of the “.mp3” in the file name?

You could split the string at the . and then use only the first part of the split array as your clip name in the loading line.

In the following you would run SplitString with your song name as the stringToSplit variable and it would return the song name without the .mp3.

string SplitString (string stringToSplit) {
    string [] split = stringToSplit.Split ('.');
    return split [0];
}
1 Like

The split thing works very well! I manage to get the title of the song without the “.mp3”.
I integrated your code in what I used like that :

public class MusicSelection : MonoBehaviour
{
    sliderManager sliderManagerScript;

    public void selectMusic()
    {
        string clipName;
        string splitedString;

        clipName = transform.GetChild(0).GetComponent<Text>().text;
        string[] split = clipName.Split('.');
        splitedString = split[0];
        Debug.Log(splitedString); //I used this line only to check the result of the previous lines
        sliderManagerScript.song = Resources.Load<AudioClip>(splitedString);
    }
}

But the problem remains…
I still get the NullReference message for the last line. I really don’t know what I’m doing wrong : /

Is your sliderManagerScript.song variable an AudioClip type? Does the sliderManagerScript even have a variable named song?

Yes, the variable song exists, it’s a public audioclip defined as my audiosource’s clip.

public class sliderManager : MonoBehaviour {

public AudioSource Source;
public AudioClip song;

    void Start()
    {
        Source = GetComponent<AudioSource>();
        Source.clip = song;
    }
}

Making the sliderManager into a singleton and referencing it that way will fix the issue.
http://wiki.unity3d.com/index.php/Singleton

public AudioSource Source;
public AudioClip song;

public static sliderManager instance;

void Awake () {
    instance = this;
}

void Start () {
    Source = GetComponent <AudioSource>();
    Source.clip = song;
}
void Awake () {
    selectMusic ();
}

public void selectMusic() {
    string clipName;
    clipName = ("My Song");
    sliderManager.instance.song = Resources.Load<AudioClip>(clipName);
}
1 Like

Everything you said worked perfectly to avoid error messages!
But I realized something :
when I try to load any audioclip, no clip is affected to the audiosource…
So now, the following line doesn’t generate any error message anymore but it doesn’t affect any clip to the audiosource neither, and it never did before I guess.

sliderManager.instance.song = Resources.Load<AudioClip>(splitedString);

Do you have any idea of what I’m doing wrong? : /
Maybe I should try another way of doing that?

which object is null?
which line error?

The "NullReferenceException "error message referes to the 10th line of the code I linked in my first message.
and the “object” concerned is an audioclip named “clipName” which I’m trying to set as my audiosource’s clip.

sliderManagerScript is null?
sliderManagerScript.song is null?
clipName is null?
Resources.Load(clipName) is null?
which?

Resources.Load(clipName) is null

AudioSource.clip =null;
AudioSource.Play();or AudioSource.PlayOneShot(null);
this is OK

so Resources.Load(clipName) is null console don’t message NullReference

Yes, I said in my last message to QuinnWinters that I realized AudioSource.clip is not set.
I want to set AudioSource.clip with a file stored in Resources folder.

my test filepath
Resources\song\123.mp3
code
Resources.Load(“song/123”) ;

“song” is actually an audio file, not a folder. I already did hat you wrote.

public class MusicSelection : MonoBehaviour
{
    sliderManager slider_Manager;

    public void selectMusic()
    {
        string clipName;
        string splitedString;

        clipName = transform.GetChild(0).GetComponent<Text>().text;
        string[] split = clipName.Split('.');
        splitedString = split[0];
        slider_Manager.song = Resources.Load<AudioClip>(splitedString);
    }
}
public class sliderManager : MonoBehaviour {

    public AudioSource Source;
    public AudioClip song;

void Start()
    {
        Source = GetComponent<AudioSource>();
        Source.clip = song;
    }
}

my english is no well
so i expressed maybe no well

this is my file path ,file “123.mp3” is in directry “song”
Resources\song\123.mp3
so load this file code is

var clip=Resources.Load<AudioClip>("song/123") ;

i think you file path is wrong

and the function “selectMusic()” is run in “Awake()”?

“Awake()” is early “Start()”

Is your AudioSource component on the same object as your sliderManager script?

Are you running the line “Source.clip = song;” after running selectMusic or does it only run in Start? If you’re only running that line in Start that is likely the source of your issue. Perhaps try running it after setting the song in selectMusic.

1 Like

Yes, it’s only running in the start. I’m gonna try to run it in the “selectMusic” method and Few other tricks, I’ll tell you if I have any advancement :slight_smile: