Music On/Off code - how to? [Resolved]

got scripts? Need scripts?
Post Reply
User avatar
Callan S.
Posts: 2043
Joined: Sun Jan 24, 2010 5:43 am

Music On/Off code - how to? [Resolved]

Post by Callan S. »

I'd like to give the player control over whether music plays or not (don't you hate being stuck with music? Or being stuck with no music!). But I don't understand the script examples and the reply on unity answers wasn't very helpful. I mean, he says 'AudioClip does not play audio, an AudioSource does' and then in the very link he gives the script example is

Code: Select all

@script RequireComponent(AudioSource)
public var otherClip: AudioClip;
Which references AudioClip and is what I basically have in the code I showed him.

Hopefully the indie resource unity forum will get some replies! :)
Last edited by Callan S. on Sun Aug 19, 2012 5:27 am, edited 1 time in total.
User avatar
Callan S.
Posts: 2043
Joined: Sun Jan 24, 2010 5:43 am

Re: Music On/Off code - how to?

Post by Callan S. »

Oookay, I don't quite understand how I fixed it, but I got another script example that did something kinda the same and worked across. I could have sworn I had this set up to begin with! Make sure an audio source is attached to whatever the script is attached to and set to play on awake and is on a loop.

Code: Select all

@script RequireComponent(AudioSource)

function OnGUI()
{
  if (GUI.Button(Rect(Screen.width-55,5,45,20),"Music"))
  	{
  	  if (audio.isPlaying)
  	  	{
  	  	audio.Pause ();
  	  	}
  	  	else
  	  	{
  	  	audio.Play ();  	  	
  	  	}
  	}
}
User avatar
Callan S.
Posts: 2043
Joined: Sun Jan 24, 2010 5:43 am

Re: Music On/Off code - how to? [Resolved]

Post by Callan S. »

Ooh, unity answers bugged me - someone gives an almost monosybilic answer, grunting that AudioSource plays the sound, not AudioClip...yet in the end I find I need to set audio.clip to, well, an audio clip. I say that's confusing and suddenly theres like a bunch of them going on about how they did answer - seriously, knowing what I know now, if I had seen my own question, I'd talk about audio.clip, not just raise a relatively non sequetuer single line answer about AudioSource. Better yet, I get scolded for giving an answer to my own question for someone else in my position, even though the software allows me to answer my own question (either stupid design or it's BS you can't answer your own question) and their own one liner and perfunctory link was no real use at all. Sorry if I tried to help out anyone else like me, unity answers! My mistake!
Post Reply

Return to “Scripting/Coding”