无法使用实例引用访问静态成员,而是使用类型名称限定它

时间:2016-02-23 13:00:13

标签: c# enums

我已经四处寻找答案,但我没有发现任何我能用来帮助解决问题的方法。 当尝试从枚举中设置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);
    }
}

1 个答案:

答案 0 :(得分:2)

而不是torchState.TurnedOff 只需使用torch.TurnedOff

喜欢

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

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