- Home /
Find an AudioClip by its variable name and play it
I have declared a bunch of AudioClips in my C# script:
public AudioClip bang;
public AudioClip pow;
public AudioClip whiz;
public AudioClip meow;
public AudioClip heartbeat;
public AudioClip knock;
public AudioClip clang;
I've got the AudioClip names being passed to this script as strings and I want to load the correct AudioClip and play it based on the incoming string.
Let's say I have:
string myString = "meow";
How can I find the corresponding AudioClip? I've been using this code, which doesn't produce any errors, but the AudioClip does not play:
AudioClip myClip = new AudioClip();
myClip = (AudioClip)Resources.Load(myString, typeof(AudioClip));
AudioSource.clip = myClip;
AudioSource.Play ();
EDIT: I understand why the above code doesn't work - the audio files are not in the project's Resources folder. I'm trying to find the AudioClip based on its variable name, not its filename and location within the project folder.
I've also tried finding it this way:
AudioClip myClip = (AudioClip)GameObject.Find ("meow");
But this throws an error because it tried to convert GameObject type to AudioClip type.
Does anyone know how to do this? Thanks!
Answer by Anxo · Dec 04, 2014 at 11:20 PM
Another perfect situation to use a dictionary, Take a look at this answer here just use one like this http://answers.unity3d.com/questions/814970/find-out-if-a-list-contains-two-objects-with-the-s.html If you want to do it right,
If you want to do it quick, just make an Array instead.
public AudioClip[] audioClips;
public void PlayThisClip(string incomeingClip){
foreach(AudioClip clip in audioClips){
audio.PlayOneShot(clip.name);
}
}
WUCC
This code confuses me as your 'incomeingClip' isn't used, this code would just play every single clip in the array. Well, it wouldn't do anything actually as it wouldn't compile, being that clip.name is a string and audio.PlayOneShot() takes the type AudioClip, not string.
I think you meant to say this:
//Using a list, could also use an array.
public List<AudioClip> clips;;
AudioSource aud = GetComponent<AudioSource>();
public void PlayAudioClip(string clipToPlay)
{
foreach (AudioClip clip in clips)
{
if(clip.name == clipToPlay)
aud.PlayOneShot(clip);
}
}
Answer by NatCou · Jan 24, 2019 at 11:57 AM
Sorry not to bring zombie post alive - but your solutions helped me get mine So I'll post in the hope it helps others. (you don't need doozy UI - to follow along)
//linked to the audio mixer and used as a sound manager // see the excellent DoozyUI examples for sound and creating a sound manager //https://www.youtube.com/watch?v=iP_9LdERGBk @15:40
public AudioSource GAME_SFX_Player;
public AudioClip UIMenuFX;
//later
public void PlayAudioClip(string clipToPlay)
{
//will play all clips
//foreach (AudioClip clip in FX_Pickups)
//{
// if (clip.name == clipToPlay)
// UI_SFX_Player.PlayOneShot(clip);
//}
UIMenuFX = (AudioClip)Resources.Load(clipToPlay, typeof(AudioClip));
GAME_SFX_Player.clip = UIMenuFX;
GAME_SFX_Player.Play();
}
Your answer
Follow this Question
Related Questions
Can you load an AudioClip from the file system w/o using WWW? 0 Answers
Trying to play two different clips from one AudioSource(not simultaneously) 1 Answer
NullReferenceException when transform.Find(string) STILL 1 Answer
Ambisonic Audio Support for PS4 0 Answers
how to stop an audio source from playing multiple times; change button text 0 Answers