Search Unity

Detecting audio file attached to audio source at runtime.

Discussion in 'Scripting' started by dijital, Oct 22, 2015.

  1. dijital

    dijital

    Joined:
    Apr 28, 2010
    Posts:
    232
    Hi,

    My audio sources are created and managed dynamically at runtime, I need to be able to detect the name of the audio file which is attached to the audio source at any time.

    I know you can detect the name of the audiosource gameobject using :

    1. if(gameobject.name.Contains("xxxxxx"))
    but I need to be able to detect the name of the actual file attached the the audio source

    Any help would be appreciated.

    Thanks
     
    Last edited: Oct 23, 2015
  2. Kona

    Kona

    Joined:
    Oct 13, 2010
    Posts:
    208
    Try caching the AudioSource and then access the .clip property of said AudioSource and then the .name property of the AudioClip.

    Code (CSharp):
    1. public AudioSource audioComponent;
    2.  
    3. public void SomeMethod(){
    4.      if( audioComponent != null && audioComponent.clip != null ){
    5.           Debug.Log( audioComponent.clip.name );
    6.      }
    7. }
     
    dijital likes this.
  3. dijital

    dijital

    Joined:
    Apr 28, 2010
    Posts:
    232
    Thanks!