Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn Sound Manager | Unity UI and Sounds
Unity for Beginners
course content

Course Content

Unity for Beginners

Unity for Beginners

1. Unity Introduction
2. Write your First Script
3. Unity Physics
4. Unity UI and Sounds
5. Polishing and Export your Game

book
Sound Manager

Understanding Singleton in Unity

In Unity, a Singleton is a design pattern that ensures a class has only one instance and provides a global point of access to it. This is particularly useful for managing game components that need to persist across different scenes, like a SoundManager.

How Singleton Works in Unity

  1. Single Instance: the SoundManager class declares a public static variable instance of its own type. This variable holds the single instance of the SoundManager class;

  2. Initialization in Start() Method: when the GameObject with this script becomes active, Unity automatically calls the Start() method. Here, the Singleton instance is initialized:

    • If instance is null, it means no SoundManager exists yet, so the current instance (this) is assigned to instance, making it the Singleton;
    • If instance is not null, another SoundManager already exists, which should not happen in a Singleton pattern. In this case, the current GameObject is destroyed using Destroy(gameObject) to maintain the Singleton pattern.
  3. Persistence Across Scenes: the method DontDestroyOnLoad(gameObject) is used to ensure that the GameObject (and thus, the SoundManager instance) persists between scene changes. This is crucial for maintaining consistent audio behavior across different scenes.

Accessing the Singleton Instance

Once set up, other scripts can easily access the SoundManager's functionality throughout the game using SoundManager.instance. For example, to play an audio effect, another script can call SoundManager.instance.PlayEffect(index). This allows any script in the game to interact with the SoundManager without creating multiple instances or worrying about initialization.

Advantages of Using Singleton

  • Centralized Management: provides a central point for managing sound-related functionality, making it easier to control and maintain audio playback across the game;

  • Global Access: the Singleton instance can be accessed globally from any script, allowing different parts of the game to interact with the sound system seamlessly;

  • Persistence: ensures consistent audio playback throughout the game without interruption during scene changes.

  • Static Instance: public static SoundManager instance; declares a static variable that will hold the single instance of the SoundManager class;

  • Start Method: the Start() method is called when the script becomes active. It checks if the instance is null (meaning no instance exists yet). If so, it assigns the current object (this) to instance, making it the Singleton;

  • Destroy Duplicate: if instance is not null, it means another instance already exists, so the current GameObject is destroyed to maintain the Singleton pattern;

  • Persistence: DontDestroyOnLoad(gameObject); ensures that the GameObject persists across scene changes, allowing the SoundManager to maintain its state and functionality throughout the game.

Accessing the Singleton Instance

Once the SoundManager is set up as a Singleton, other scripts can easily access its functionality throughout the game: Other scripts can access the SoundManager instance using SoundManager.instance. For example, to play an audio effect, another script can call SoundManager.instance.PlayEffect(index).

This way, any script in the game can access the SoundManager instance and its functionality without needing to create multiple instances or worrying about initialization.

Advantages in Unity

Using the Singleton pattern in Unity, especially for managers like the SoundManager, offers several benefits:

Centralized Management: it provides a central point for managing sound-related functionality, making it easier to control and maintain audio playback across the game.

Global Access: the Singleton instance can be accessed globally from any script, allowing different parts of the game to interact with the sound system seamlessly.

Persistence: the Singleton instance persists across scenes, ensuring consistent audio playback throughout the game without interruption during scene changes.

1. What is the purpose of the instance variable in the SoundManager class?

2. What does DontDestroyOnLoad(gameObject) do in the SoundManager class?

What is the purpose of the instance variable in the `SoundManager` class?

What is the purpose of the instance variable in the SoundManager class?

Select the correct answer

What does `DontDestroyOnLoad(gameObject)` do in the `SoundManager` class?

What does DontDestroyOnLoad(gameObject) do in the SoundManager class?

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 4. Chapter 5
We're sorry to hear that something went wrong. What happened?
some-alt