c# - Adding audioClip from the Resources folder through code -


how can add , play short audioclip name in code in unity? tested lot of samples internet doesn't work.

    audiosource audio = gameobject.addcomponent<audiosource>();     audio.play((audioclip)resources.load("clip1"));​ 

to second line:

assets/resources/clickaction.cs(14,55): error cs1525: unexpected symbol `'

enter image description here

audiosource.play() not take audioclip parameter.

audiosource.playoneshot() does. pawel talked except no code example provided in answer.

this play prototype looks like:

public void play(); public void play(ulong delay); 

none of them takes audioclip parameter.

so should be:

audiosource audio = gameobject.addcomponent<audiosource>(); audio.playoneshot((audioclip)resources.load("clip1")); 

you can still use the play() function question must first assign audiosource.clip (audioclip)resources.load("clip1"); before calling play() function.

so, should work too:

audiosource audio = gameobject.addcomponent<audiosource>(); audio.clip = (audioclip)resources.load("clip1"); audio.play(); 

Comments

Popular posts from this blog

php - How to display all orders for a single product showing the most recent first? Woocommerce -

asp.net - How to correctly use QUERY_STRING in ISAPI rewrite? -

angularjs - How restrict admin panel using in backend laravel and admin panel on angular? -