Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Callan S. · Aug 18, 2012 at 03:23 AM · audiopauseplaymusiccontrol

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 :)

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image AlucardJay · Aug 19, 2012 at 05:12 AM 0
Share
 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

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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 ();          
       }
    }
 }


Comment
Add comment · Show 6 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image AlucardJay · Aug 19, 2012 at 05:57 AM 1
Share

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();       
         }
     }
 }
avatar image Paulo-Henrique025 · Aug 19, 2012 at 03:02 PM 1
Share

Do not post your results as a new answer, this si not the Unity forum. Aways use comments for something like that.

avatar image Callan S. · Aug 20, 2012 at 12:29 AM 0
Share

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.

avatar image fafase · Aug 20, 2012 at 06:50 AM 1
Share

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.

avatar image Paulo-Henrique025 · Aug 20, 2012 at 04:22 PM 1
Share

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.

Show more comments
avatar image
1

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

Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Callan S. · Aug 19, 2012 at 12:10 AM 0
Share

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.

avatar image Paulo-Henrique025 · Aug 19, 2012 at 03:01 PM 1
Share

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.

avatar image AlucardJay · Aug 19, 2012 at 04:30 PM 0
Share

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.

avatar image Callan S. · Aug 20, 2012 at 12:33 AM 0
Share

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

avatar image AlucardJay · Aug 20, 2012 at 06:43 AM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

11 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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

Is there any way to make the headphone pause/unpause controls work when the Unity app is running, to pause/unpause background app(apple music/google music) music? Android app. 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges