player prefs using toggles

here I have a toggle when I press it the music turns off and when i press it again it turns back on using player prefs I want to make my game remember if it was of or on my main problem is "cannot convert bool to string.

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

            PlayerPrefs.SetString("Muted", muted2);
        }
        else
        {
            GameObject.FindWithTag("Music").GetComponent<AudioSource>().mute = false;
            PlayerPrefs.SetString("NotMuted", muted2);
        }
    }

There are many potential solutions, but converting bool to string or int is perhaps easiest done with the ternary operator.

PlayerPrefs.SetInt("Muted", audioSource.mute ? 1 : 0);

However, when you decode you instead must check bool isMuted = PlayerPrefs.GetInt("Muted") != 0;

I am not sure if you could make your original solution work even if you solved the converison. Since you set both Muted and NotMuted at some point you will notice that both are set, and then it will be hard to know what the correct setting is.

do you think changing them both to be either “muted” or “notmuted” is a good idea

I would use ”muted” as the only option. That seems easiest.

You should not splatter string literals all over your program, such as “Muted” and “NotMuted”.

For one it makes a colossal mess of your code, for two it is dangerous if you mistype one, or if one gets changed and the others don’t.

Instead, centralize it with a simple wrapper class like this and use the property only:

Here’s an example of simple persistent loading/saving values using PlayerPrefs:

Useful for a relatively small number of simple values.

It even shows a wrapped example of precisely what @ubbelito suggests above.