Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
avatar image
0
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
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

0 Replies

· Add your reply
  • Sort: 

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.

Follow this Question

Answers Answers and Comments

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

Related Questions

Audio while player is moving 0 Answers

Audio Mixer exposed parameter path error? 0 Answers

Why the audio sound effects of the main menu are not hearing like in mute ? 0 Answers

Only one audio active. Others do not activate 2 Answers

GetSpectrumData over entire file 1 Answer


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