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 /
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
0
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
Add comment · 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
1
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
Add comment · 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 ;)

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

Add Audio Source to a Prefab 2 Answers

Scroll rect sound problem 0 Answers

How to play multiple audioclips from the same object? 4 Answers

How Do I Get This To Play Sound? 0 Answers

How can I Get An Audio Clip to Play a Certain Number Of Times C# 2 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