- Home /
Music On/Off Switch
I'm attempting to add a control so users can turn the music off or on. After researching this code seems to be like other answers here, but it gives an error message.
var musicname : AudioClip;
function OnGUI()
{
if (GUI.Button(Rect(Screen.width-50,5,45,20),"Music"))
{
if (musicname.isPlaying)
{
musicname.Pause (musicname);
}
else
{
musicname.Play (musicname);
}
}
}
The error message it gives when clicked is:
MissingMethodException: Method not found: 'UnityEngine.AudioClip.Play'.
Boo.Lang.Runtime.DynamicDispatching.MethodDispatcherFactory.ProduceExtensionDispatcher ()
Boo.Lang.Runtime.DynamicDispatching.MethodDispatcherFactory.Create ()
Boo.Lang.Runtime.RuntimeServices.DoCreateMethodDispatcher (System.Object target, System.Type targetType, System.String name, System.Object[] args)
Boo.Lang.Runtime.RuntimeServices.CreateMethodDispatcher (System.Object target, System.String name, System.Object[] args)
Boo.Lang.Runtime.RuntimeServices+<Invoke>c__AnonStorey12.<>m__6 ()
Boo.Lang.Runtime.DynamicDispatching.DispatcherCache.Get (Boo.Lang.Runtime.DynamicDispatching.DispatcherKey key, Boo.Lang.Runtime.DynamicDispatching.DispatcherFactory factory)
Boo.Lang.Runtime.RuntimeServices.GetDispatcher (System.Object target, System.String cacheKeyName, System.Type[] cacheKeyTypes, Boo.Lang.Runtime.DynamicDispatching.DispatcherFactory factory)
Boo.Lang.Runtime.RuntimeServices.GetDispatcher (System.Object target, System.Object[] args, System.String cacheKeyName, Boo.Lang.Runtime.DynamicDispatching.DispatcherFactory factory)
I have dragged the name of the audio clip into the inspector slot for it. Any help appreciated - as we all know, it's nice to be able to control the music in a game and I'd like to give that option :)
var music1 : AudioClip;
var music2 : AudioClip;
var music3 : AudioClip;
audio.clip = music1;
// OR : audio.clip = music2;
// OR : audio.clip = music3;
audio.Play();
Scroll down to my answer on this question ($$anonymous$$ $$anonymous$$ay): http://answers.unity3d.com/questions/250261/press-key-p-the-object-will-random-to-play-music.html
Answer by Callan S. · Aug 19, 2012 at 05:50 AM
Oookay, after further reasearch 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 the code set up like this to begin with! Putting this here for anyone else looking for an answer instead of a link. 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.
@script RequireComponent(AudioSource)
function OnGUI()
{
if (GUI.Button(Rect(Screen.width-55,5,45,20),"Music"))
{
if (audio.isPlaying)
{
audio.Pause ();
}
else
{
audio.Play ();
}
}
}
As Paulo and myself have tried to explain, AudioClip does not play audio, an AudioSource does. Your first script would have worked fine if you had considered this :
@script RequireComponent(AudioSource)
var musicname : AudioClip;
// somewhere in the script, probably at start :
audio.clip = musicname;
function OnGUI()
{
if ( GUI.Button(Rect(Screen.width-50,5,45,20),"$$anonymous$$usic") )
{
if (audio.isPlaying)
{
audio.Pause();
}
else
{
audio.Play();
}
}
}
Do not post your results as a new answer, this si not the Unity forum. Aways use comments for something like that.
And as I've tried to explain, that isn't an answer - as long as you have an audiosource attached (which I always have, from the start), you don't even need the required component part. So how does directing me to AudioSource help, when even your own answer revolves around AudioClip/audio.clip? It's like I'm saying my brake and accelerator isn't working and your telling me engine makes the car go. Sure, now let's talk about how to make the accelerator/break tell the engine to go or stop. As to not posting results as a new answer, why does the script here allow me to do it if I am not to do it? At best it's a double message.
Ok, when you use
AudioSource.Play(sound);
Ins$$anonymous$$d of using the audio source attached to the object you instantiate a new one. AudioSource is a constructor for an audio source component. This audio source will not be attached to the object and you can see it in the Hierarchy showing up and disappearing when the sound is done. The object lives the length of the sound. That is how you fixed your issue.
An audio source is a component, an audio clip is a variable used by the audio source to create a sound.
double message? You don't even know where you are, you should be glad that a Q&A is superficial and does not have a "report button". I'm unsubscribing from this question.
Answer by Paulo-Henrique025 · Aug 18, 2012 at 03:49 AM
AudioClip does not play audio, an AudioSource does.
http://docs.unity3d.com/Documentation/ScriptReference/AudioSource-clip.html
Further information: I tried 'var musicname :AudioSource' previously and in the inspector it would not even let me drag wav or midi files into it. Further the page link you gave has an example which uses AudioClip as I do? That's really confusing.
No, the implementation is different, you seems to not understad how audio works in Unity, please take a more careful look at the documentation i've linked. The Play() function is aways called in the AudioSource, which is a component that the object must have to play audio clips.
http://docs.unity3d.com/Documentation/Components/class-AudioSource.html
http://docs.unity3d.com/Documentation/Components/class-AudioClip.html
Imagine the AudioSource is a $$anonymous$$P3 Player, and the AudioClip is a song on the Player ( var musicname : AudioClip; )
So you tell the Player what song to get ready ( audio.clip = musicname; )
Then press play ( audio.Play(); )
O$$anonymous$$? so AudioSource is the speakers where the sound comes out, AudioClip is what the sound is.
An AudioSource has always been attached to the object (the same object the script was attached to) - right from the start I've had it playing on awake, cheerily playing music. It's just my button would not control it. Have people assumed I didn't have an audio source attached when giving their answer? If so, that assumption makes things confusing.
At best you want to say that audio.clip is the wire that goes to the speakers and I need to use that particular wire/plug things into that wire. Just telling me about the speakers doesn't explain it, particularly when the answer is audio.CLIP but people keep refering to an AudioSOURCE as somehow being the answer. The speakers aren't the answer. "I got a red and a green wire, which do I cut?" "The bomb! Look at the bomb!" :o
was just merely trying to make an analogy that would hopefully show the difference between the AudioSource component and the audio.clip , but if you just want to rant in your frustration then forget it. Talk about failing to recognize when people are trying to help. This was a real fun message to wake up to =[
Your answer
Follow this Question
Related Questions
How to make camera position relative to a specific target. 1 Answer
how to pause and resume music after pausing game? 4 Answers
Different music for pause menu? 1 Answer
Why sound doesn't stop? 2 Answers