tmighty
tmighty

Reputation: 11399

struct inaccessible due to its protection level

I have declared a private struct inside a class.

When I try to use it, the compiler raises the error

struct inaccessible due to its protection level

This is the C# code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;

public class HUDanimator : MonoBehaviour
{
    private struct udtThis
    {
        Color col1;
        Color col2;
        float wait;
        float fade;
    }

    private udtThis[] m = new udtThis[2];

    void Start()
    {
        udtThis n; //raises the compiler error
        n.wait = 0f; 

What am I doing wrong here?

Thank you.

Upvotes: 2

Views: 3123

Answers (2)

Ondrej Tucny
Ondrej Tucny

Reputation: 27962

Most likely your compiler complains at the n.wait = 0f; line, because the struct's fields are private. Make them public:

private struct udtThis
{
    public Color col1;
    public Color col2;
    public float wait;
    float fade;
}

Then your code sample compiles just fine.

Upvotes: 5

Isitar
Isitar

Reputation: 1449

You can make the properties in the struct public or internal and access them the normal way.

I recommend encapsulating them like this:

    public Color Col1 { get; set; }
    public Color Col2 { get; set; }
    public float Wait { get; set; }
    public float Fade { get; set; }

Upvotes: 3

Related Questions