- Home /
Question by
unity_KPzA8Ravb2HEzQ · Jan 03, 2019 at 05:48 AM ·
scripting problemaudioscript.audiosourcemusic
Glitched Music
I am having trouble with my music in my game, I don't know I've tried everything to fix my main menu music, but every time I pause or it goes into the game over screen my main menu music won't stop. This started after I added an extra variable in my boss script so it can have boss music, I know this is the problem but I am not sure what, here are the scripts where music is called, I am very sure it's the first two scripts but i'm including the rest anyway.
Level Manger script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class LevelManager : MonoBehaviour {
public float waitToRespawn;
public PlayerController thePlayer;
public GameObject deathSplosion;
public int coinCount;
private int coinBonusLifeCount;
public int bonusLifeThreshold;
public AudioSource coinSound;
public AudioSource lifeSound;
public AudioSource extraLifeSound;
public Text coinText;
public Image heart1;
public Image heart2;
public Image heart3;
public Sprite heartFull;
public Sprite heartHalf;
public Sprite heartEmpty;
public int maxHealth;
public int healthCount;
private bool respawning;
public ResetOnRespawn[] objectsToReset;
public bool invincible;
public Text livesText;
public int startingLives;
public int currentLives;
public GameObject gameOverScreen;
public AudioSource levelMusic;
public AudioSource gameOverMusic;
public bool respawnCoActive;
// Use this for initialization
void Start() {
thePlayer = FindObjectOfType<PlayerController>();
healthCount = maxHealth;
objectsToReset = FindObjectsOfType<ResetOnRespawn>();
if(PlayerPrefs.HasKey("CoinCount"))
{
coinCount = PlayerPrefs.GetInt("CoinCount");
}
coinText.text = "COINS: " + coinCount;
if (PlayerPrefs.HasKey("PlayerLives"))
{
currentLives = PlayerPrefs.GetInt("playerLives");
} else {
currentLives = startingLives;
}
currentLives = startingLives;
livesText.text = "Lives x " + currentLives;
}
// Update is called once per frame
void Update() {
if(healthCount <= 0 && !respawning)
{
respawning = true;
Respawn();
}
if(coinBonusLifeCount >= bonusLifeThreshold)
{
currentLives += 1;
livesText.text = "Lives x " + currentLives;
coinBonusLifeCount -= bonusLifeThreshold;
extraLifeSound.Play();
}
}
public void Respawn()
{
currentLives -= 1;
livesText.text = "Lives x " + currentLives;
if (currentLives > 0)
{
StartCoroutine("RespawnCo");
} else {
thePlayer.gameObject.SetActive(false);
gameOverScreen.SetActive(true);
levelMusic.Stop();
gameOverMusic.Play();
}
}
public IEnumerator RespawnCo()
{
respawnCoActive = true;
thePlayer.gameObject.SetActive(false);
Instantiate(deathSplosion, thePlayer.transform.position, thePlayer.transform.rotation);
yield return new WaitForSeconds(waitToRespawn);
respawnCoActive = false;
healthCount = maxHealth;
respawning = false;
UpdateHeartMeter();
coinCount = 0;
coinText.text = "COINS: " + coinCount;
coinBonusLifeCount = 0;
thePlayer.transform.position = thePlayer.respawnPostion;
thePlayer.gameObject.SetActive(true);
for (int i = 0; i < objectsToReset.Length; i++)
{
objectsToReset[i].gameObject.SetActive(true);
objectsToReset[i].ResetObject();
}
}
public void AddCoins(int coinsToAdd)
{
coinCount += coinsToAdd;
coinBonusLifeCount += coinsToAdd;
coinText.text = "COINS: " + coinCount;
coinSound.Play();
}
public void HurtPlayer(int damageToTake)
{
if (!invincible)
{
healthCount -= damageToTake;
UpdateHeartMeter();
thePlayer.Knockback();
thePlayer.hurtSound.Play();
}
}
public void GiveHealth(int healthToGive)
{
healthCount += healthToGive;
if(healthCount > maxHealth)
{
healthCount = maxHealth;
}
lifeSound.Play();
UpdateHeartMeter();
}
public void UpdateHeartMeter()
{
switch (healthCount)
{
case 6:
heart1.sprite = heartFull;
heart2.sprite = heartFull;
heart3.sprite = heartFull;
return;
case 5:
heart1.sprite = heartFull;
heart2.sprite = heartFull;
heart3.sprite = heartHalf;
return;
case 4:
heart1.sprite = heartFull;
heart2.sprite = heartFull;
heart3.sprite = heartEmpty;
return;
case 3:
heart1.sprite = heartFull;
heart2.sprite = heartHalf;
heart3.sprite = heartEmpty;
return;
case 2:
heart1.sprite = heartFull;
heart2.sprite = heartEmpty;
heart3.sprite = heartEmpty;
return;
case 1:
heart1.sprite = heartHalf;
heart2.sprite = heartEmpty;
heart3.sprite = heartEmpty;
return;
case 0:
heart1.sprite = heartEmpty;
heart2.sprite = heartEmpty;
heart3.sprite = heartEmpty;
return;
default:
heart1.sprite = heartEmpty;
heart2.sprite = heartEmpty;
heart3.sprite = heartEmpty;
return;
}
}
public void AddLives(int livesToAdd)
{
extraLifeSound.Play();
currentLives += livesToAdd;
livesText.text = "Lives x " + currentLives;
}
}
Boss Script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Boss : MonoBehaviour {
public bool bossActive;
public float timeBetweenDrops;
private float timeBetweenDropsStore;
private float dropCount;
public float waitForPlatforms;
private float platformCount;
public Transform leftPoint;
public Transform rightPoint;
public Transform dropSawSpawnPoint;
public GameObject dropSaw;
public GameObject theBoss;
public bool bossRight;
public GameObject rightPlatforms;
public GameObject leftPlatforms;
public bool takeDamage;
public int StartingHealth;
private int currentHealth;
public GameObject levelExit;
public AudioSource MightyFoe;
private LevelManager theLevelManager;
public bool playBossMusic = false;
public bool waitingForRespawn;
// Use this for initialization
void Start () {
timeBetweenDropsStore = timeBetweenDrops;
dropCount = timeBetweenDrops;
platformCount = waitForPlatforms;
currentHealth = StartingHealth;
theBoss.transform.position = rightPoint.position;
bossRight = true;
theLevelManager = FindObjectOfType<LevelManager>();
}
// Update is called once per frame
void Update() {
if (theLevelManager.respawnCoActive)
{
bossActive = false;
waitingForRespawn = true;
MightyFoe.Stop();
theLevelManager.levelMusic.Play();
}
if(waitingForRespawn && !theLevelManager.respawnCoActive)
{
theBoss.SetActive(false);
leftPlatforms.SetActive(false);
rightPlatforms.SetActive(false);
timeBetweenDrops = timeBetweenDropsStore;
platformCount = waitForPlatforms;
dropCount = timeBetweenDrops;
theBoss.transform.position = rightPoint.position;
bossRight = true;
currentHealth = StartingHealth;
waitingForRespawn = false;
}
if (bossActive)
{
theBoss.SetActive(true);
if (!playBossMusic)
{
theLevelManager.levelMusic.Stop();
MightyFoe.Play();
playBossMusic = true;
}
if (dropCount > 0)
{
dropCount -= Time.deltaTime;
}
else
{
dropSawSpawnPoint.position = new Vector3(Random.Range(leftPoint.position.x, rightPoint.position.x), dropSawSpawnPoint.position.y, dropSawSpawnPoint.position.z);
Instantiate(dropSaw, dropSawSpawnPoint.position, dropSawSpawnPoint.rotation);
dropCount = timeBetweenDrops;
}
if (bossRight)
{
if (platformCount > 0)
{
platformCount -= Time.deltaTime;
} else {
rightPlatforms.SetActive(true);
}
} else {
if (platformCount > 0)
{
platformCount -= Time.deltaTime;
}
else
{
leftPlatforms.SetActive(true);
}
}
if (takeDamage)
{
currentHealth -= 1;
if(currentHealth <= 0)
{
levelExit.SetActive(true);
gameObject.SetActive(false);
MightyFoe.Stop();
theLevelManager.levelMusic.Play();
}
if (bossRight)
{
theBoss.transform.position = leftPoint.position;
} else {
theBoss.transform.position = rightPoint.position;
}
bossRight = !bossRight;
rightPlatforms.SetActive(false);
leftPlatforms.SetActive(false);
platformCount = waitForPlatforms;
timeBetweenDrops = timeBetweenDrops /2f;
takeDamage = false;
}
}
else
{
if(!theLevelManager.levelMusic.isPlaying)
{theLevelManager.levelMusic.Play(); }
MightyFoe.Stop();
playBossMusic = false;
}
}
private void OnTriggerEnter2D(Collider2D other)
{
if(other.tag == "Player")
{
bossActive = true;
}
}
}
Game Over Screen Script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class GameOver : MonoBehaviour {
public string levelSelect;
public string mainMenu;
private LevelManager theLevelManager;
// Use this for initialization
void Start () {
theLevelManager = FindObjectOfType<LevelManager>();
}
// Update is called once per frame
void Update () {
}
public void Resart()
{
PlayerPrefs.SetInt("CoinCount", 0);
PlayerPrefs.SetInt("PlayerLives", theLevelManager.startingLives);
SceneManager.LoadScene(SceneManager.GetActiveScene().name);
}
public void LevelSelect()
{
PlayerPrefs.SetInt("CoinCount", 0);
PlayerPrefs.SetInt("PlayerLives", theLevelManager.startingLives);
SceneManager.LoadScene(levelSelect);
}
public void QuitToMainMenu()
{
SceneManager.LoadScene(mainMenu);
}
}
Pause Screen Script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class PauseScreen : MonoBehaviour {
public string levelSelect;
public string mainMenu;
private LevelManager theLevelManager;
public GameObject ThePauseScreen;
private PlayerController thePlayer;
// Use this for initialization
void Start () {
theLevelManager = FindObjectOfType<LevelManager>();
thePlayer = FindObjectOfType<PlayerController>();
}
// Update is called once per frame
void Update () {
if (Input.GetButtonDown("Pause"))
{
if(Time.timeScale == 0f)
{
ResumeGame();
} else {
PauseGame();
}
}
}
public void PauseGame()
{
Time.timeScale = 0;
ThePauseScreen.SetActive(true);
thePlayer.canMove = false;
theLevelManager.levelMusic.Pause();
}
public void ResumeGame()
{
ThePauseScreen.SetActive(false);
Time.timeScale = 1f;
thePlayer.canMove = true;
theLevelManager.levelMusic.Play();
}
public void LevelSelect()
{
PlayerPrefs.SetInt("PlayerLives", theLevelManager.currentLives);
PlayerPrefs.SetInt("CoinCount", theLevelManager.coinCount);
Time.timeScale = 1f;
SceneManager.LoadScene(levelSelect);
}
public void QuitToMainMenu()
{
Time.timeScale = 1f;
SceneManager.LoadScene(mainMenu);
}
}
Comment