-1

I am making a Pause menu in Unity that appears when you press escape. But whenever I press escape, the background music still plays. I am getting an error in lines 32 and 46. The error log is Assets\PauseMenu.cs(32,32): error CS0029: Cannot implicitly convert type 'UnityEngine.AudioSource' to 'UnityEngine.AudioSource[]'

Here is my code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PauseMenu : MonoBehaviour
{
    public static bool GameIsPaused = false;
    public GameObject pauseMenuUI;

    // Update is called once per frame
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.Escape))
        {
            if (GameIsPaused)
            {
                Resume();
            }
            else
            {
                Pause();
            }
        }
    }

    void Resume()
    {
        pauseMenuUI.SetActive(false);
        Time.timeScale = 1f;
        GameIsPaused = false;

        AudioSource[] audios = FindObjectOfType<AudioSource>();

        foreach (AudioSource a in audios)
        {
            a.Play();
        }
    }

    void Pause()
    {
        pauseMenuUI.SetActive(true);
        Time.timeScale = 0f;
        GameIsPaused = true;

        AudioSource[] audios = FindObjectOfType<AudioSource>();

        foreach (AudioSource a in audios)
        {
            a.Pause();
        }
    }
}
fatihyildizhan
  • 8,614
  • 7
  • 64
  • 88
  • [`FindObjectsOfType()`](https://docs.unity3d.com/ScriptReference/Object.FindObjectOfType.html) .. note the **s**! – derHugo Apr 25 '21 at 08:42

1 Answers1

0

FindObjectOfType<AudioSource>() return only one object to return an array of object use FindObjectsOfType<AudioSource>()

Art Zolina III
  • 497
  • 4
  • 10