• 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 /
  • Help Room /
avatar image
0
Question by martipello · Feb 22, 2016 at 01:47 PM · audiosourceaudioclipbool

turning off effects and music seperately one works the other wont?

long title but it serves my purpose. im turning on and off the music and sound effects seperately im using the same method for both, but the effects turn on and off fine and the music refuses to, my effects are all played using play one shot after checking with a public static bool, my music is playing using

 GetComponent<AudioSource>().Play();

the bool works for each if i click the ui buttons for music or effects they turn off and on, and if i change the bool in my script to false for each of them the music and effects dont play but from the ui buttons its only the effects that turn off and on, im not sure what to add to this question to make it clearer so ill start with my scripts and if theres anything else thats relevant please let me know and ill add it

here is the script with the bools for music and effects that is turned off and on by the ui buttons ( script edited to be shorter)

 public class allGlobals

 public static bool music = true;
 public static bool fx = true;

  public void musicOn()
 {
     if (music)
         music = false;
     else if (!music)
         music = true;
 }
 
 public void fxOn()
 {
     if (fx)
         fx = false;
     else if (!fx)
         fx = true;
 }

and heres the script to turn the music on and off which im pretty sure is the problem but im not sure why, as stated if i set the other bool to false the music doesnt play (script not edited)

 public class mainMusic : MonoBehaviour {
 
 public AudioClip clip;
 // Use this for initialization
 void Awake () {
 }
 // Update is called once per frame
 void Start()
 {
     if (allGlobals.music == true)
     {
         GetComponent<AudioSource>().clip = clip;
         GetComponent<AudioSource>().Play();
         GetComponent<AudioSource>().loop = true;
     }
 }
 } 

ive tried changing this script such as adding the other half of the if statement something like this

public class mainMusic : MonoBehaviour {

 public AudioClip clip;
 // Use this for initialization
 void Awake () {
 }
 // Update is called once per frame
 void Start()
 {
     if (allGlobals.music)
     {
         GetComponent<AudioSource>().clip = clip;
         GetComponent<AudioSource>().Play();
         GetComponent<AudioSource>().loop = true;
     }
    else if (!allGlobals.music)
     {
         GetComponent<AudioSource>().Stop();
     }
 }
 } 

but again no joy the bool changes to false on click like it should but the music just keeps playing. Im still a bit of a noob so hopefully this is an easy fix ive tried changing the loop to false also thank you for any and all suggestions

Comment
Add comment
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

1 Reply

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

Answer by martipello · Feb 22, 2016 at 02:01 PM

okay really sorry not the first time ive done this, asked a question and then almost immediately solved it, ive worked it out i need to pause and unpause in update(), so this is now working for me

 using UnityEngine;
 using System.Collections;

 public class mainMusic : MonoBehaviour {
 
 public AudioClip clip;
 // Use this for initialization
 void Awake () {
     
 }

 // Update is called once per frame
 void Start()
 {
     if (allGlobals.music == true)
     {
         GetComponent<AudioSource>().clip = clip;
         GetComponent<AudioSource>().Play();
         GetComponent<AudioSource>().loop = true;
     }
 }
     void Update(){
     if (allGlobals.music == true)
     {
         GetComponent<AudioSource>().UnPause();
         GetComponent<AudioSource>().loop = true;
     }

     if (!allGlobals.music)
     {
         GetComponent<AudioSource>().loop = false;
         GetComponent<AudioSource>().Pause();
     }

 }
 
   }

again my apologies but maybe itll help someone down the line

Comment
Add comment · 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

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.

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

33 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 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

Audio sliders? 0 Answers

I get: 'UnassignedReferenceException' when trying to play a sound clip though a sound source, yet i am assigning it. HELP?! 0 Answers

Second AudioClip won't play 0 Answers

My sound effect is not playing 0 Answers

How to make an editor extension to unity ui for switching audio clips? 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