On each level a random song is played through a function. Except Startscreen, which has a specific track. Here’s all the relevant code.
var myMusic : AudioClip[];
var menuMusic : AudioClip;
function playRandomMusic()
{
audio.clip = myMusic[Random.Range(0, myMusic.length)];
audio.Play();
}
function playMenuMusic () {
audio.clip = menuMusic;
audio.Play();
}
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’
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.
hpjohn
October 22, 2014, 12:08pm
2
var myMusic : AudioClip[];
var menuMusic : AudioClip;
var songIndex : int;
var songName : string;
function playRandomMusic()
{
songIndex = Random.Range(0, myMusic.length);
songName = myMusic[songIndex].name;
audio.clip = myMusic[songIndex];
audio.Play();
}
function playMenuMusic () {
audio.clip = menuMusic;
audio.Play();
}
GUI.Label(Rect(0,10,100,20),"audio = "+songName);
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
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.
i think this might work idk
var myMusic : AudioClip[];
var menuMusic : AudioClip;
function playRandomMusic()
{
audio.clip = myMusic[Random.Range(0, myMusic.length)];
audio.Play();
}
function playMenuMusic () {
audio.clip = menuMusic;
audio.Play();
}
function findMusicName() : string
{
foreach(ac : AudioClip in myMusic)
if(audio.clip == ac) return ac.name;
return "Nothing to see here";
}