- Home /
Resume Button Sound Not Playing
I've been trying to get the resume button in a menu to make a click sound, I've tried through the unity UI and also through scripting but nothing has given me results. I left the resume button audio enabled to see if disabling the object was the problem but nothing happened.
Here's my code below, I commented the hopefully related parts
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
public class GameMenu : MonoBehaviour
{
public static bool GamePauseState = false;
public GameObject pauseMenuUI;
public GameObject gameOverScreen;
public GameObject winScreen;
public AudioSource gameOverSound;
public AudioSource menuButton1;
public AudioSource menuButton2;
public AudioSource menuButton3;
public AudioSource nextLevelButton;
public AudioSource quitButton1;
public AudioSource quitButton2;
public AudioSource quitButton3;
public AudioSource resumeButton; // ResumeButton audioSource
public AudioClip resumeSound; // ResumeButton Sound
void Start()
{
gameOverSound.ignoreListenerPause = true;
menuButton1.ignoreListenerPause = true;
menuButton2.ignoreListenerPause = true;
menuButton3.ignoreListenerPause = true;
nextLevelButton.ignoreListenerPause = true;
quitButton1.ignoreListenerPause = true;
quitButton2.ignoreListenerPause = true;
quitButton3.ignoreListenerPause = true;
resumeButton.ignoreListenerPause = true; // Lets audio play when paused
}
void Update()
{
if (Input.GetKeyDown(KeyCode.Escape))
{
if (GamePauseState)
{
Resume();
}
else
{
Pause();
}
}
if(GameController.Instance.die)
{
AudioListener.pause = true;
gameOverScreen.SetActive(true);
}
if (GameController.Instance.win)
{
AudioListener.pause = true;
winScreen.SetActive(true);
}
}
public void Resume()
{
resumeButton.enabled = true; // Keeps audio source from being disabled
resumeButton.PlayOneShot(resumeSound, 1.0f); // Plays audio
pauseMenuUI.SetActive(false);
Time.timeScale = 1f;
GamePauseState = false;
AudioListener.pause = false;
}
Answer by Saniell · Jul 11, 2019 at 02:29 PM
I can assume that your "resumeButtonSource" is a child of "pauseMenuUI" and you turn off your audiosource with: pauseMenuUI.SetActive(false);
So sound can not be played.
It is good idea to actually do unified Audio System that is independent on the state of the gameobjects
Your answer
Follow this Question
Related Questions
Anchor Stretch Problem 0 Answers
WebGL and Audio Support! 0 Answers
Play sound on button click before loading level. 3 Answers
Toggle SOUND from main menu and in-game (iOS) 2 Answers
Pause: Issues with pausing, resuming, and pressing buttons within pause 2 Answers