静态成员不能使用实例引用访问,请改用类型名称限定它

本文关键字:类型 不能 实例 引用 静态成员 访问 | 更新日期: 2023-09-27 18:35:46

我已经四处寻找答案,但我还没有找到任何我可以用来帮助解决我的问题的答案。尝试从枚举在 c# 中设置火炬的状态时,我收到错误消息;

静态成员"TorchManager.torch.TurnedOff"无法使用实例引用访问,请改用类型名称对其进行限定

我已经尝试序列化枚举并使用静态变量作为枚举引用,但似乎没有任何效果。

这是我的代码;

public class TorchManager : MonoBehaviour
{
    enum torch
    {
        TurnedOff,
        TurnedOn,
        Flickering,
        Resetting
    }
    torch torchState;
    float torchTimer = 0.0f;
    float torchTimerMax = 180.0f;
    float torchFlickerTimer = 00f;
    float flickerRate = 0.1f;
    private float startIntensity = 1.0f;
    float maxFlickerIntensity = 1.8f;
    bool useBatteries = true;
    int batteryCount = 2;
    int batteryValue = 1;

    void Start ()
    {
        startIntensity = GetComponent<Light>().intensity;
        torchState = torch.TurnedOff;
    }
    void Update ()
    {
        CheckForInputs();
        RunFlashlight();
    }
    void CheckForInputs()
    {
        if(Input.GetKeyDown(KeyCode.F))
        {
            if(torchState == torchState.TurnedOff && batteryCount > 0)
            {
                torchState = torch.TurnedOn;
            }
            else if(torchState == torchState.TurnedOn || torchState == torch.Flickering)
            {
                torchState = torch.TurnedOff;
            }
        }
        if(Input.GetKeyDown(KeyCode.B) && batteryCount > 0)
        {
            if(useBatteries)
            {
                batteryCount -= 1;
            }
            torchTimer = 0.0f;
            torchState = torch.Resetting;
        }
    }
    void RunFlashlight()
    {
        switch (torchState)
        {
        case torch.TurnedOff :
            GetComponent<Light>().enabled = false;
            torchFlickerTimer = 0f;
            break;
        case torch.TurnedOn :
            GetComponent<Light>().enabled = true;
            torchTimer += Time.deltaTime;
            if(torchTimer >= torchTimerMax)
            {
                torchState = torch.Flickering;
                torchTimer = 0.0f;
            }
            break;
        case torch.Flickering :
            torchFlickerTimer += Time.deltaTime;
            torchTimer += Time.deltaTime;
            if(torchTimer > flickerRate)
            {
                float lightIntensity = Random.Range(0.0f,maxFlickerIntensity);
                GetComponent<Light>().intensity = lightIntensity;
                torchTimer = 0.0f;
            }
            if(torchFlickerTimer >= 15f)
            {
                torchState = torch.TurnedOff;
            }
            break;
        case torch.Resetting :
            torchTimer += Time.deltaTime;
            if(torchTimer > 0.75f)
            {
                GetComponent<Light>().intensity = startIntensity;
                torchTimer = 0.0f;
                torchState = torch.TurnedOn;
            }else if(torchTimer > 0.55f)
            {
                GetComponent<Light>().intensity = 0.0f;
            }else if(torchTimer > 0.35f)
            {
                GetComponent<Light>().intensity = startIntensity;
            }else if(torchTimer > 0.15f)
            {
                GetComponent<Light>().intensity = 0.0f;
            }
            break;
        }
    }
    void OnGUI()
    {
        GUI.Box(new Rect(20, 120, 125, 25), "Batteries in torch: " + batteryCount);
    }
}

静态成员不能使用实例引用访问,请改用类型名称限定它

而不是 torchState.TurnedOff只需使用手电筒即可。已关闭

喜欢在

if(torchState == torchState.TurnedOff && batteryCount > 0)
{
    torchState = torch.TurnedOn;
}

torchState == torchState.TurnedOff更改为torchState == torch.TurnedOff