I'm stuck here because there are some error

public static void LevelUnlocked(int level){
the error box say "you can’t use the same members type"
but why?

full code

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

    public class PlayerPrefsManager : MonoBehaviour {

        const string MASTER_VOLUME_KEY = "master_volume";
        const string DIFFICULTY_KEY = "difficulty";
        const string LEVEL_KEY = "level_unlocked_";

    public static void SetMasterVolume(float volume)
        {
            if (volume > 0f && volume < 1f) { 
            PlayerPrefs.SetFloat(MASTER_VOLUME_KEY,volume);
            }
            else
            {
                Debug.LogError("master volume out of range");
            }
        }
        public static float GetMasterVolume()
        {
            return PlayerPrefs.GetFloat(MASTER_VOLUME_KEY);
        }
    }    // this brace is currently closing the class definition

        // the following code is outside of the class definition
        public static void LevelUnlocked(int level)
        {
         if(level <=Application.levelCount - 1)
        {
            PlayerPrefs.SetInt(LEVEL_KEY + level.ToString(),1);
        }
        else
        {
            Debug.LogError("level locked ");
        }
1 Like

Error CS0116

1 Like

H Francesco,

That’s a syntax issue.

If you take a look at your brackets ( ) and braces {} you will invariably spot the problem.

Take a look at the SetMasterVolume method, you have an additional } which is effectively closing the class definition - so the compiler is telling you that the rest of the syntax after that brace just doesn’t make any sense.

You also have a closing brace missing from the LevelUnlocked method, in fact, if I had to guess, I’d say that you added that last method afterwards, placing it after the original closing class definition brace.

To resolve, just move that spare brace to the bottom of the class, job done.

Going forward, I would recommend using more of a block format for the brace, e.g.

private void Start()
{
    // ...
}

rather than;

private void Start() {
    // ...
}

It does make them a little bit easier to read/spot as they should be lining up vertically in your code, worth perhaps trying whilst you are still getting used to things.

You can do the same for the if statements also;

private void Start()
{
    if(blockStyleBraces == true)
    {
        // my code might be easier to read
    }
    else
    {
        // ooh, its gunna be harder to spot them
    }
}

Also, if you use three ` characters before and after your code, when you paste it into the forum, it will be formatted correctly, as code, and the using statements will be includes. I have updated your post, but consider checking out the link below.

Hope this helps :slight_smile:


See also;

ooooh Thank you !!! you saved me :smiley:

1 Like

You’re very welcome, glad you can move forward again :slight_smile:

1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms