I have a music sound clip simply drag-n-dropped to my hierarchy. I also have other sound effects in my game. I want to pause/play background music on button press but only music, not all sound effects. How can I reach this music GameObject in C# code?


2 Answers 2


You can access any object in your hierarchy by searching for it:

GameObject soundObject = GameObject.Find("BackgroundSoundObjectName");

Then you're likely going to want to access the AudioSource component:

AudioSource audioSource = soundObject.GetComponent<AudioSource>();

Then you can use the Pause() and Play() methods of the audio source to control the music.


If you have multiple audio sources, FindObjectsOfType might be useful (The UnityEngine.Object part is unnecessary if called from a MonoBehaviour-inherited script):

AudioSource[] allAudioSources =

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .