Build a Simple In-Game Radio in Unity
Is your game world feeling a little… silent? Does your player character yearn for a bit of sonic companionship during those long virtual drives or solitary explorations? Then it’s time to inject some life into your Unity project with a functional in-game radio. This isn’t just about adding background music; it’s about crafting an interactive element that enhances immersion and provides a diegetic source of entertainment.
Here’s how to ditch the silent treatment and get your radio waves flowing, focusing on a practical, script-driven approach perfect for smaller projects. We’re skipping the complex audio middleware for now; let’s get the basics nailed down.
Setting Up Your Audio Sources
The most-downloaded assets on Wayline this month — sorted by what real developers actually use.
The core of our radio is the AudioSource component. This is where the magic (and the sound) happens. You’ll need one AudioSource per radio station you want to implement.
- Create an empty
GameObjectin your scene (e.g., “Radio”). - Add an
AudioSourcecomponent to it. Name it "Station1", and repeat for each station. - Disable “Play On Awake” on each
AudioSource. We only want them playing when we tell them to. - Assign your audio clips (the music for each station) to the “Audio Clip” field of their respective
AudioSourcecomponents. Ensure your audio clips are looped.
Pro Tip: Experiment with spatial blend settings on your AudioSource. Setting it closer to 3D allows for subtle directional audio, adding to the realism when the player moves around the radio.
Building the UI for Station Selection
A radio needs a dial! Or at least, some buttons. Unity’s UI system will handle this.
- Create a
Canvasin your scene (GameObject > UI > Canvas). - Inside the Canvas, create a series of
Buttonelements (GameObject > UI > Button). - Label each button with the name of a station (e.g., "Rock Station", “Talk Radio”).
- Position and style the buttons to your liking. A simple horizontal layout works well.
- Consider a slider for volume control.
Common Pitfall: Forgetting to scale your UI correctly for different screen resolutions. Use the Canvas Scaler component (set to “Scale With Screen Size”) to ensure your UI remains consistent across devices.
Scripting the Radio Logic
This is where the real work happens. We need a script to handle button presses and switch between audio sources.
using UnityEngine;
using UnityEngine.UI;
public class RadioController : MonoBehaviour
{
public AudioSource[] stations; // Array of AudioSources for each station
public Button[] stationButtons; // Array of Buttons to switch stations
private int currentStationIndex = -1;
void Start()
{
// Assign click listeners to buttons
for (int i = 0; i < stationButtons.Length; i++)
{
int stationIndex = i; // Capture the index for the lambda expression
stationButtons[i].onClick.AddListener(() => SwitchStation(stationIndex));
}
}
void SwitchStation(int stationIndex)
{
// Stop the current station
if (currentStationIndex >= 0 && currentStationIndex < stations.Length)
{
stations[currentStationIndex].Stop();
}
// If selected station is already playing, stop it
if (currentStationIndex == stationIndex) {
currentStationIndex = -1;
return;
}
// Start the new station
currentStationIndex = stationIndex;
stations[currentStationIndex].Play();
}
}
Explanation:
stations: An array to hold references to all ourAudioSourcecomponents (the stations). Drag and drop your "Station1", "Station2", etc.GameObjectsinto this array in the Inspector.stationButtons: An array to hold references to the buttons in the UI.Start(): This function iterates through thestationButtonsarray and adds a listener for each button click. The lambda expression captures the indexiso each button press knows which station to switch to.SwitchStation(int stationIndex): This function stops the currently playing station (if any), then starts the station corresponding to the button pressed. The check will stop it if it is playing.
Challenges: Null reference exceptions are common here. Double-check that all AudioSource and Button references are correctly assigned in the Inspector.
Value Beyond Surface Level: Adding a simple on/off state using the same button for example, like our SwitchStation method shows.
Connecting the Script
- Create a new C# script named
RadioController. - Copy and paste the code above into the script.
- Attach the
RadioControllerscript to the “Radio”GameObject. - In the Inspector panel of the
RadioControllerscript, you’ll see thestationsandstationButtonsarrays. Drag and drop yourAudioSourceGameObjects into thestationsarray, and your UIButtonGameObjects into thestationButtonsarray. Make sure the order matches! Station 1’s button should correspond to Station 1’sAudioSource.
Now, when you run your game and click the buttons, you should hear different radio stations!
Adding Volume Control
You can easily add volume control by referencing the AudioSource.volume property. Link a slider to the AudioSource and update the volume from 0 to 1 based on the slider value.
Final Thoughts: This simple in-game radio system is a stepping stone. From here, you can expand it with features like procedural audio generation, streaming internet radio, or even implementing a system that changes stations based on in-game events. Now get those frequencies flowing!