• 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 /
This question was closed Mar 16, 2015 at 01:29 AM by KnightRiderGuy for the following reason:

The question is answered, right answer was accepted

avatar image
Question by KnightRiderGuy · Jan 22, 2015 at 10:29 PM · c#audiosourcebuttonsaudioplay

How to get sound to play and pause with the new UI

I'm trying to have a sound play on an empty game object using a UI button with the new UI system. I basically have this script on an empty game object with a sound file as sound source. But when I ad another button state and try and use the stop sound the button does not work at all, but I can get it to play with the first play sound option. I'm not good at C# so I'm sure I'm buggering this up with the new UI somehow. using UnityEngine; using System.Collections;

 public class PodScannerSoundManager : MonoBehaviour {
 
 
     public void ScannerPlay()
     {
         audio.Play();
         
     }
 
     public void ScannerStop()
     {
         audio.Stop();
         
     }
 }
Comment

People who like this

0 Show 3
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 khos85 · Jan 22, 2015 at 10:45 PM 0
Share

In the button have you added the event trigger script and added your function there?

avatar image KnightRiderGuy · Jan 22, 2015 at 11:48 PM 0
Share

![buttons][1]

Yup I did that and both don't work, if I remove the bottom one then the 2nd one down works?? [1]: /storage/temp/39347-screen-shot-2015-01-22-at-54619-pm.png

screen-shot-2015-01-22-at-54619-pm.png (22.9 kB)
avatar image Superrodan · Jan 23, 2015 at 12:07 AM 0
Share

I'm a little confused by the setup in the screenshot. It seems you have one button with three commands. The first command is telling an audiosource to play, and the second is telling something to both stop and play at the same time maybe?

If you want one button to control one sound then you only need one thing to trigger onClick. The other button should handle turning the sound off in it's onClick.

1 Reply

  • Sort: 
avatar image
Best Answer

Answer by InvincibleCat · Jan 23, 2015 at 01:25 AM

It's normal because you are calling all your method on Click. So it will call audio.Stop() in the end...

Call just one (and only one) function from the button:

 public void TogglePlay()
 {
      if(audio.isPlaying)
      {
         audio.Stop();
      }
      else
      {
         audio.Play();
      }
 }

Cheers

Comment
Ika21

People who like this

1 Show 2 · 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 KnightRiderGuy · Jan 23, 2015 at 01:42 AM 0
Share

Ah, perfect, that did the trick. Thanks InvincibleCat I had been at that one for the better part of the day ;)

avatar image InvincibleCat · Jan 23, 2015 at 01:42 AM 0
Share

You are welcome ;)

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

21 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to play audio through 7.1 speakers correctly? 1 Answer

audio clip 1 plays on enter clip 2 plays on exit but needs clip 1 to stop on exit 1 Answer

Remote KeyPad with Buttons 1 Answer

Looking for a method for implementing multiple random sound arrays. 2 Answers

Play audio in reverse (needed to "scratch") 3 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