c# - 由于其保护级别而无法访问结构

标签 c# class unity3d struct

我在一个类中声明了一个私有(private)结构。

当我尝试使用它时,编译器抛出错误

struct inaccessible due to its protection level

这是 C# 代码:

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; 

我在这里做错了什么?

谢谢。

最佳答案

您的编译器很可能会在 n.wait = 0f; 行报错,因为该结构的字段是私有(private)的。公开它们:

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

然后您的代码示例就可以正常编译了。

关于c# - 由于其保护级别而无法访问结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53362921/

相关文章:

c# - UWP ListView 按钮 MVVM 绑定(bind)

c# - 如何使用 Visual C# 从 SQL Server 表中获取 ID 号

c# - for循环跳过一行

html - 引用 CSS 类

c# - Unity C#无法从迭代器返回值。使用yield return语句返回一个值,或使用yield break结束迭代

c# - 在 .net 中将 MIDI 转换为 .wav 文件

java - 启动 Activity 的 Intent 不起作用

c++ - 是否可以编写一个抽象类,其中构造函数选择适当的子类在 C++ 中实例化?

c# - Unity - 为摩托车使用刚体 - 我该如何转弯?

windows - 建成后无雾