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
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
Your answer
Follow this Question
Related Questions
Newbie with OnTriggerEnter & Audio Files 0 Answers
I want the audio to play when the player nears the object, but fade away when they walk away. 0 Answers
Determine end of audio clip if paused and resumed inbetween? 1 Answer
Object Reference not set to an instance of an object 0 Answers
Any way to code skipping song after pressing key on keyboard ? 1 Answer