"Mute ""Music"" tag" (860323)

I have to toggles on my main menu one that mutes sound “MuteToggle” and one that should just mute Music “MusicToggle” the music toggle is meant to mute audio with the tag “music” and it doesn’t. It only mutes the music one the first scene and not the rest

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

public class MuteAudio : MonoBehaviour
{
    public void MuteToggle(bool muted)
    {
        if (muted)
        {
            AudioListener.volume = 0;
        }
        else
        {
            AudioListener.volume = 1;
        }
    }


    public void MusicToggle(bool muted2)
    {
        if (muted2)
        {
            GameObject.FindWithTag("Music").GetComponent<AudioSource>().mute = true;
        }
        else
        {
            GameObject.FindWithTag("Music").GetComponent<AudioSource>().mute = false;
        }
    }
}

Admittedly I don’t normally mess with options menus much but I imagine playerprefs would be a fairly easy fix for this problem, you’ll need to look into how to carry the data you want across multiple scenes for the entire game. For more complicated or permanent options look up serialisation and saving data to files so that your game remembers this selection every time it’s loaded up.

1 Like

thanks i hate menus to

GameObject.FindWithTag("Music") It only finds the GameObject with the Tag Music
And it can be any game object as long as it has the tag Music.
Advice: Don’t use GameObject.Find nor FindWithTag. Serialize the components instead.
Or use UnityEvent<bool> and then serialize the AudioSource to mute it.

What @Lethn already suggested is the use of PlayerPrefs. This is an API that is made for saving user settings.
Now player prefs doesn’t have a boolean variant. But what you can do is save it as an integer.

Set
PlayerPrefs.SetInt("UserSettings.MuteMusic", value ? 1 : 0)

Get (with default value 0)
PlayerPrefs.GetInt("UserSettings.MuteMusic", 0) == 1

When the Toggle changes value use the Set.
When you Awake the Music playing component you can use Get.

If you already have an instance in the scene at that time, you can simply serialize this in the Toggle and call the AudioSource component directly to change the mute property.

2 Likes

I’ve tried this and it doesn’t work

    public void MusicToggle(int muteMusic)
    {
        if (muteMusic)
        {
            PlayerPrefs.GetInt("UserSettings.MuteMusic", 0) == 1;
        }
        else
        {
            PlayerPrefs.SetInt("UserSettings.MuteMusic", value ? 1 : 0);
        }
    }

FindWithTag only finds 1 GameObject in the hierarchy matching that tag. It is not capable of returning more than 1. Instead you can use FindGameObjectsWithTag to return all GameObjects in the hierarchy matching that tag as an array. Then you can loop through the entire array and mute them all.

https://docs.unity3d.com/ScriptReference/GameObject.FindGameObjectsWithTag.html

But it can only find GameObjects in the hierarchy, meaning it cannot find GameObjects in scenes which are not currently running. If you want to do your music mute system this way, you’ll want to call it again when the scene changes.

I instead often create a music manager object with DontDestroyOnLoad set, so you just need to set volume or mute once instead of on every scene load, though doing it on every scene load can work as well.