Error CS0176

Hello, I needed to use PlayerPrefs.SetBool() and then I realised there’s no such thing, so I’ve found a work around, which was this method http://wiki.unity3d.com/index.php?title=BoolPrefs I made a new script, copied and pasted the whole code given in the link and now I get this error

“Assets/Scripts/interaction.cs(18,37): error CS0176: Static member `PlayerPrefsX.SetBool(string, bool)’ cannot be accessed with an instance reference, qualify it with a type name instead”

I tried to google for the solution but none of the posts appeared to be relevant to my issue, I’m pretty sure it’s just like a one letter or word in the code that is causing this problem but I can’t find it :frowning:

using UnityEngine;
using System.Collections;

public class PlayerPrefsX : MonoBehaviour {

    public static void SetBool(string name, bool booleanValue)
    {
        PlayerPrefs.SetInt(name, booleanValue ? 1 : 0);
    }
   
    public static bool GetBool(string name) 
    {
        return PlayerPrefs.GetInt(name) == 1 ? true : false;
    }
   
    public static bool GetBool(string name, bool defaultValue)
    {
        if(PlayerPrefs.HasKey(name)) //Error here
        {
            return GetBool(name);
        }
       
        return defaultValue;
    }
}

Thanks!

The error is in your interaction class not PlayerPrefsX.

Gosh…how did I not notice that…Anyway, here’s the interaction code, still have no clue what’s wrong with it.

using UnityEngine;
using System.Collections;

public class interaction : MonoBehaviour {
    public enabler enabled;
    public textBoxManager textBox;
    public PlayerPrefsX playerPrefs;

    // Use this for initialization
    void Start () {
   
    }
   
    // Update is called once per frame
    void Update () {
        if (Input.GetKey ("Space") && enabled.triggered == true) {
            textBox.EnableTextBox();
            playerPrefs.SetBool("pickedUpWeapon", true); //Error here
        }
    }
}

SetBool is a static method so you’d call it with

PlayerPrefsX.SetBool(....);

Given that everything is static (unless there’s more to that class that you didn’t post) then there’s no reason for the class to not be static and therefore for it to inherit from MonoBehaviour.

1 Like

Yes, that fixed it. Thank you very much for solving it and explaining what was wrong.