0
\$\begingroup\$

Is there anyway I can get an audioclip by the name it has in the assets folder? Or will I always have to make a reference to it by dragging and dropping the clip on a SoundClip in the inspector?

\$\endgroup\$
2
  • \$\begingroup\$ If you put your sounds into the Resources folder they can be loaded by name via the Resources.Load() command. \$\endgroup\$
    – Calvin
    Commented Oct 19, 2013 at 20:02
  • \$\begingroup\$ That is very interesting. Thanks. However i meant that if i have audioClip called "DeathScream05" id like to play it with something like: audio.PlayOneShot("DeathScream05"); \$\endgroup\$
    – Daarwin
    Commented Oct 19, 2013 at 20:51

1 Answer 1

0
\$\begingroup\$

There is no way to achieve this out of the box. Unity requires an AudioClip reference for the PlayOneShot method.

You'll need to write a simple audio manager to achieve this, or simply include a reference to the clip in your script.

\$\endgroup\$
3
  • \$\begingroup\$ Any tips on how to get started with a audiomanager? \$\endgroup\$
    – Daarwin
    Commented Oct 20, 2013 at 9:21
  • \$\begingroup\$ Create an AudioManager prefab and instantiate it when you load your scene. This prefab will contain your script component. You'll want to create a public Array of audio clips to expose to the editor (so that you can easily drop them in in the inspector). in Awake(), run through your array/list and add them to a Dictionary<string, AudioClip> Lastly, create a static method PlayByName(string clipName) or whatever you want to call it. Then this method can get the AudioClip out of the Dictionary using it's name as the key. Hope this helps. \$\endgroup\$
    – raybarrera
    Commented Oct 20, 2013 at 9:44
  • \$\begingroup\$ Great! Got it. :) \$\endgroup\$
    – Daarwin
    Commented Oct 20, 2013 at 10:55

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .