I’m trying to debug a problem where at the beginning of our game, a bunch of sounds play. It would be nice if I could just see, with the game paused, which audio clips are currently playing on ANY game object at all. Is there a way to see this? Otherwise I can only debug this by doing a tedious binary search of disabling various game objects to track it down.
You can try listing all the game objects whose audio sources are currently playing in the debug console. That way you at least know the names of the objects that are playing audio at any given point.
Try something like this:
Attach this script to any game object.
using System;
using UnityEngine;
public class PlayingList : MonoBehaviour
{
AudioSource[] sources;
void Start () {
//Get every single audio sources in the scene.
sources = GameObject.FindSceneObjectsOfType(typeof(AudioSource)) as AudioSource[];
}
void Update () {
// When a key is pressed list all the gameobjects that are playing an audio
if(Input.GetKeyUp(KeyCode.A))
{
foreach(AudioSource audioSource in sources)
{
if(audioSource.isPlaying) Debug.Log(audioSource.name+" is playing "+audioSource.clip.name);
}
Debug.Log("---------------------------"); //to avoid confusion next time
Debug.Break(); //pause the editor
}
}
}
This should list the name of all the game objects which are playing audio and also the name of the clips that is played. should then be easy to figure out which ones are causing the problem (unless many of your game objects share the same name, which is not a good idea).
Hope this helps.
So, I decided to throw together a little script that I think will work quite nicely. Haven’t checked it if it works per-say, but it throws no errors. Hope this helps, if not, hopefully helping someone else.
using System;
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
/*
* To use this, put this in a file named ListPlaying.cs.
* Place all objects inside of an empty game object -- For ease.
* Place ListPlaying.cs on that empty game object.
*/
public class ListPlaying : MonoBehaviour
{
public List<string> playing = new List<string>();
public string compiledList = "";
public void OnGUI()
{
GUI.Label(new Rect(0,0, Screen.height, Screen.width), compiledList);
}
public void Update()
{
foreach(Transform child in transform)
{
if(child.GetComponent<AudioSource>() != null)
{
if(child.audio.isPlaying)
playing.Add("" + child.ToString());
else
playing.Remove("" + child.ToString());
}
}
compiledList = "";
foreach(string song in playing)
{
compiledList = compiledList + song + "
";
}
}
}
Ah good ideas. I found it the old fashioned way, but this is definitely good for future reference.