This question was
closed Apr 14, 2017 at 05:47 PM by
dgalbraith3 for the following reason:
I figured it out.
Question by
dgalbraith3 · Apr 12, 2017 at 05:51 PM ·
uibuttonaudiosoundaudio source
C# On Click sound to play before loading scene
I have looked at other questions asked but couldn't find anything related with how I set up my menu. I have a script called "UI Click" and add script to button.
[RequireComponent(typeof(Button))] public class UIClick : MonoBehaviour {
public AudioClip sound;
private Button button { get { return GetComponent<Button>(); } }
private AudioSource source { get { return GetComponent<AudioSource>(); } }
// Use this for initialization
void Start () {
gameObject.AddComponent<AudioSource>();
source.clip = sound;
source.playOnAwake = false;
button.onClick.AddListener(() => PlaySound());
}
// Update is called once per frame
void Update () {
}
void PlaySound()
{
source.PlayOneShot(sound);
}
}
Code button is using to load level:
public class ButtonManager : MonoBehaviour { void Start() {
Cursor.visible = true;
Cursor.lockState = CursorLockMode.None;
AudioListener.pause = false;
//Make sure time is normal when game is starting
Time.timeScale = 1; //setting time back to normal
}
public void LoadScene(string newGameLevel)
{
SceneManager.LoadScene(newGameLevel);
}
}
I am still learning to code so I am not sure what to do to get it to finish playing the whole sound before loading the next level.
Comment