Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by aihodge · Dec 04, 2014 at 11:10 PM · audiostringaudioclipfind

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!

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image kalibcrone · Jun 05, 2017 at 08:19 PM 1
Share

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);
             }
         }
 
 
 
avatar image
1

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();
     }


Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges