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

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

jsf - "PropertyNotWritableException: Illegal Syntax for Set Operation" error when setting value in bean -

laravel - Undefined property: Illuminate\Pagination\LengthAwarePaginator::$id (View: F:\project\resources\views\admin\carousels\index.blade.php) -