由于其保护级别,结构无法访问

tmi*_*hty 2 c# struct class unity-game-engine

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

当我尝试使用它时,编译器会引发错误

struct inaccessible due to its protection level
Run Code Online (Sandbox Code Playgroud)

这是 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; 
Run Code Online (Sandbox Code Playgroud)

我在这里做错了什么?

谢谢。

Ond*_*cny 5

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

private struct udtThis
{
    public Color col1;
    public Color col2;
    public float wait;
    float fade;
}
Run Code Online (Sandbox Code Playgroud)

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