Question error CS0122

RedHand

New member
Joined
Mar 1, 2021
Messages
3
Programming Experience
Beginner
I'm writing a code that is trying to access the backSprite and frontSprite, but it gives me an error CS0122. Can anyone help?
This is for Unity. The error is: Assets\Script\Battle\BattleUnit.cs(16,57): error CS0122: 'MonsterBase.frontSprite' is inaccessible due to its protection level
and Assets\Script\Battle\BattleUnit.cs(14,57): error CS0122: 'MonsterBase.backSprite' is inaccessible due to its protection level
C#:
public class BattleUnit : MonoBehaviour
{
    [SerializeField] MonsterBase _base;
    [SerializeField] int level;
    [SerializeField] bool isPlayerUnit;
   
    public Monster monster { get; set; }


    public void Setup()
    {
        monster = new Monster(_base, level);
        if (isPlayerUnit)
            GetComponent<Image>().sprite = monster.Base.backSprite;
        else
            GetComponent<Image>().sprite = monster.Base.frontSprite;
    }

C#:
public class MonsterBase : ScriptableObject
{
    [SerializeField] string name;

    [TextArea]
    [SerializeField] string description;

    [SerializeField] Sprite backSprite;
    [SerializeField] Sprite frontSprite;
 
Last edited:
How about you provide the actual error message, rather expecting us to look it up for ourselves? You could also tell us what line(s) the error was generated on. It might also be worthwhile specifying what technology this relates to. You talk about sprites so it is something specific. DirectX or Unity something else graphics-related, I presume.

I have also formatted your code appropriately. Please do so for us in future.
 
How about you provide the actual error message, rather expecting us to look it up for ourselves? You could also tell us what line(s) the error was generated on. It might also be worthwhile specifying what technology this relates to. You talk about sprites so it is something specific. DirectX or Unity something else graphics-related, I presume.

I have also formatted your code appropriately. Please do so for us in future.
thanks! I'll edit the question.
 
Now that I know what I'm looking for, the issue is obvious. Every kind of member (field, property, method, etc) has a default access level based on the kind of type it belongs to (class or structure). You have declared fields in a class and so the default access level is private. Unless you explicitly declare those fields with an access level, they will be private. If you expect to access them outside the type, that's obviously no use. You need to explicitly declare them public.

In fact, you should never simply accept the default access level and always explicitly specify the access level you want, even if it is the same as the default. That way, you never have to remember what the defaults are and you can never get it wrong. Also, no one reading the code (which might be you later on) needs to wonder whether you specifically wanted the default access level or just forgot to specify something else.

Generally speaking, it is considered good practice in .NET to keep read/write fields private and expose data publicly via properties. I'm not sure whether that convention extends to Unity.
 
Now that I know what I'm looking for, the issue is obvious. Every kind of member (field, property, method, etc) has a default access level based on the kind of type it belongs to (class or structure). You have declared fields in a class and so the default access level is private. Unless you explicitly declare those fields with an access level, they will be private. If you expect to access them outside the type, that's obviously no use. You need to explicitly declare them public.

In fact, you should never simply accept the default access level and always explicitly specify the access level you want, even if it is the same as the default. That way, you never have to remember what the defaults are and you can never get it wrong. Also, no one reading the code (which might be you later on) needs to wonder whether you specifically wanted the default access level or just forgot to specify something else.

Generally speaking, it is considered good practice in .NET to keep read/write fields private and expose data publicly via properties. I'm not sure whether that convention extends to Unity.
Thank you
 

Latest posts

Back
Top Bottom