Home > Blockchain >  Sound is not playing After Gameover Unity
Sound is not playing After Gameover Unity

Time:08-24

Idk why the sound is not playing when the game is over. I am able to play sounds on the game menu and when the game start but when the game is over the sound doesn't play. I am sure i am making some mistake but i am not able to find where i am doing wrong. any help will be appreciated. Here is the code

public static GameManager instance;
public GameObject PlatformSpawner;
public GameObject GameTapTextPanel;
public GameObject GameIntroPanel;
public GameObject scoreManagerUI;
public bool gameStarted;

public bool gameEnd;
// Start is called before the first frame update
public Text scoreText;
public Text highScoreText;
int score = 0;
int highScore;

AudioSource audioSource;
public AudioClip[] GameMusic;

void Awake()
{
    if (instance == null)
    {
        instance = this;
    }

    audioSource = GetComponent<AudioSource>();
}

void Start()
{
    highScore = PlayerPrefs.GetInt("HighScore");
    highScoreText.text = "Best Score : "   highScore;
}

// Update is called once per frame
void Update()
{
    if (!gameStarted)
    {
        if (Input.GetMouseButtonDown(0))
        {
            GameStart();
            audioSource.clip = GameMusic[1];
            audioSource.Play();
        }
    }
}

public void GameStart()
{
    gameStarted = true;
    PlatformSpawner.SetActive(true);
    StartCoroutine(ScoreUp());    // to update the score we need to add this code;
    GameTapTextPanel.SetActive(false);
    scoreManagerUI.SetActive(true);
    GameIntroPanel.GetComponent<Animator>().Play("TwistTurnAnim");
}

public void GameOver()
{
    audioSource.clip = GameMusic[3];  //Here is the problem the sound doesn't play
    audioSource.Play();

    gameStarted = false;
    gameEnd = true;
    PlatformSpawner.SetActive(false);
    scoreManagerUI.SetActive(true);
    GameIntroPanel.SetActive(false);
    SaveHighScore();
    Invoke("ReloadLevel",1f);
    StopCoroutine(ScoreUp());
    
}

void ReloadLevel()
{
    SceneManager.LoadScene("Game");
}

//this one is used to increase score per second of the game;
IEnumerator ScoreUp()
{
    while (true)
    {
        yield return new WaitForSeconds(1.5f);
        score   ;
        scoreText.text = score.ToString();
    }
}

public void IncreamentScore()
{
    audioSource.PlayOneShot(GameMusic[2],0.4f);
    score  = 4; //Bonus points for game
    scoreText.text = score.ToString();
}


void SaveHighScore()
{
    if (PlayerPrefs.HasKey("HighScore"))
    {
        if (score > PlayerPrefs.GetInt("HighScore"))
        {
            PlayerPrefs.SetInt("HighScore", score);
        }
    }
    else
    {
        PlayerPrefs.SetInt("HighScore",score);
    }
}

CodePudding user response:

Is the audio source part of an object that is deleted upon the Game Over condition?

The scripting API mentions that AudioSource is attached to an object.

CodePudding user response:

In your method, GameOver, you are calling Invoke("ReloadLevel",1f) which will reload the scene in 1.0 seconds. Your GameMusic[3] is not playing because it doesn't have a chance to play before the scene is reloaded.

  • Related