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.
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.
Thanks for your answer QuinnWinters 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 :
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?
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 : /
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.
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.
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.
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.