Set up your audio to be addressables. Use the string as the addressable path and load up the audioclip and pass that to your audiosource. At least, that’s how I did it in a recent project. But, you really just need a way to associate your string with the proper audioclip.
Essentially you pass in the addressable path and it will return the audioclip tied to that path. Note that you can change the addressable path to whatever you want, you don’t have to use the default it puts in for you.
AudioSource.clip is of type AudioClip, you are assigning a value of type string to it. AudioClips are not strings, therefor you cant make the assignment to that variable.
you need something like
audioSource.clip = GetClipFromString("cloud");
AudioClip GetClipFromString(string clipName){
//return the AudioClip associated with clipName
}
this function is an example of an association from string to AudioClip that Brethnann was referring to. The simplest way for you to acheive your goal may be the following, but i dont think it is very scaleable and wouldn’t use it for 10000 clips.
public class AudioClipGetter{
[SerializeField] List<AudioClip> clips; //add your clips to this list in inspector
Dictionary<string, AudioClip> dict;
void Awake(){
dict = new Dictionary<string, AudioClip>();
for(int i = 0; i < clips.Count; i++){
AudioClip clip = clips[i];
dict[clip.name] = clip;
}
}
//this is your association function
public AudioClip GetClip(string clipName){
if(dict.ContainsKey(clipName)) return dict[clipName];
else return null;
}
}
Again this is not very scaleable, but should do the job.
Yes that populates the dictionary. if by second line, you mean the ‘GetClip’ function, that is how you will be able to get an audioClip from a string, allowing you to perform
notice the return type of the function is of type AudioClip, making it a valid value to assign to audioSource.clip.
I added some more notes to the class for you or anyone in the future stumbling upon this.
public class AudioClipGetter{
//add your clips to this list in inspector
[SerializeField] List<AudioClip> clips;
//this is the the object that will hold a mapping of strings to AudioClips
Dictionary<string, AudioClip> dict;
void Awake(){
//initialize an empty dictionary, so we can add clip names and their associated AudioClips to it
dict = new Dictionary<string, AudioClip>();
//add all clips to the dictionary, such that they string that will be used to be associated
//with them will be the name of the clip file (i.e. 'cloud.mp3')
for(int i = 0; i < clips.Count; i++){
AudioClip clip = clips[i];
dict[clip.name] = clip;
}
}
//this is your association function. you provide it the name of the clip (i.e. 'cloud.mp3')
//as the parameter to the function. It returns the clip with that name, if you added that clips
//to the serialized list field 'clips'.
public AudioClip GetClip(string clipName){
//check that there is a clip associated with clip
if(dict.ContainsKey(clipName)) return dict[clipName];
else return null;
}
}
ahh, that is what actually creates the mapping in the dictionary between clip.name and the AudioClip. I editted that part of the code in my response because there was a missing line of code. check out “for loop” and “Dictionary” in C# (but they are popular in most languages) if any of it seems foreign to you.