Search Unity

Find out what random song is playing

Discussion in 'Scripting' started by san.daniele, Oct 22, 2014.

  1. san.daniele

    san.daniele

    Joined:
    Aug 16, 2013
    Posts:
    19
    On each level a random song is played through a function. Except Startscreen, which has a specific track. Here's all the relevant code.

    Code (JavaScript):
    1.  
    2. var myMusic : AudioClip[];
    3. var menuMusic : AudioClip;
    4.  
    5. function playRandomMusic()
    6. {
    7.     audio.clip = myMusic[Random.Range(0, myMusic.length)];
    8.     audio.Play();
    9. }
    10.  
    11. function playMenuMusic () {
    12.    audio.clip = menuMusic;
    13.    audio.Play();
    14. }
    15.  
    works like a charm. Now I'd like some other things to happen if a certain song is playing. How can I find out which song is playing?

    I thought the song name will be stored in the audio.clip and can be addressed through it. But this returns me 'null'

    Code (JavaScript):
    1. GUI.Label(Rect(0,10,100,20),"audio = "+audio.clip.ToString());
    In the menu it works and it shows me the name of the song, it's just the random function that I can't figure out.
     
  2. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
    Code (CSharp):
    1. var myMusic : AudioClip[];
    2. var menuMusic : AudioClip;
    3. var songIndex : int;
    4. var songName : string;
    5.  
    6. function playRandomMusic()
    7. {
    8.    songIndex = Random.Range(0, myMusic.length);
    9.     songName = myMusic[songIndex].name;
    10.     audio.clip = myMusic[songIndex];
    11.     audio.Play();
    12. }
    13. function playMenuMusic () {
    14.    audio.clip = menuMusic;
    15.    audio.Play();
    16. }
    17.  
    18.  
    19. GUI.Label(Rect(0,10,100,20),"audio = "+songName);
     
  3. ZO5KmUG6R

    ZO5KmUG6R

    Joined:
    Jul 15, 2010
    Posts:
    490
    audio.clip.name

    Not audio.clip.ToString();

    ToString shouldn't be used for stuff such as names because of stuff like:
    Texture2D.name
    AudioClip.name
    Texture.name
    GameObject.name

    etc
     
  4. san.daniele

    san.daniele

    Joined:
    Aug 16, 2013
    Posts:
    19
    thx a lot hpjohn, works great (typo in the "string" variable definition).

    I'm not 100% sure I understand the need of those extra 2 variables instead of just reading the number that's assigned to audio.clip … gonna try to wrap my brain around that.

    @aaro4130:
    you're right (even though that wasn't the cause of the problem here).
    just copy/pasted the code from another script without thinking.
     
  5. Cpt Chuckles

    Cpt Chuckles

    Joined:
    Dec 31, 2012
    Posts:
    86
    i think this might work idk
    Code (csharp):
    1.  
    2. var myMusic : AudioClip[];
    3. var menuMusic : AudioClip;
    4.  
    5. function playRandomMusic()
    6. {
    7.     audio.clip = myMusic[Random.Range(0, myMusic.length)];
    8.     audio.Play();
    9. }
    10.  
    11. function playMenuMusic () {
    12.    audio.clip = menuMusic;
    13.    audio.Play();
    14. }
    15.  
    16. function findMusicName() : string
    17. {
    18.    foreach(ac : AudioClip in myMusic)
    19.       if(audio.clip == ac) return ac.name;
    20.  
    21.    return "Nothing to see here";
    22. }