Unity静态成员`UpgradeManager.tickValue'无法使用实例引用访问,请使用类型名称限定它

时间:2016-10-12 13:31:23

标签: unity3d static structure

如果tickValue是静态的,我如何保持这样的结构?

    BufferedReader read = null;
    Process process = null;
    StringBuffer sb = new StringBuffer();
    try {
        process = new ProcessBuilder(new String[]{"/bin/bash", "-c", command}).start();
        //stderr
        printExecStdErr(command, process);
        //stdout
        read = new BufferedReader(new InputStreamReader(process.getInputStream()));
        String line = "";
        int lineNumber = 1;
        while ((line = read.readLine()) != null) {
            if (lineNumber++ > 1) {
                sb.append(System.lineSeparator());
            }
            sb.append(line);
        }
        String result=sb.toString();
        logger.info("excute command:{} result:{}",command,result);
        try {
            boolean success = process.waitFor(3000, TimeUnit.MILLISECONDS);
            if (!success){
                logger.warn("execute command:{} waitFor 3 seconds,timeout!");
                return "";
            }
        } catch (InterruptedException e) {
           logger.error("execute command:{} interrupted exception!");
        }
        logger.info("excute command:{} success",command);
        return result;
    } catch (IOException e) {
      logger.error("execute command:{} error:{}",command,e.getMessage(),e);
    } finally {
        if (null != read) {
            try {
                read.close();
            } catch (IOException e) {
            }
        }
        if (process != null){
            process.destroy();
        }
    }

1 个答案:

答案 0 :(得分:0)

此错误表示您的UpgradeManager如下所示

public class UpgradeManager 
{
    public static float tickValue;
}

删除static关键字,它将在您提问的上下文中有效。

如果要在静态上下文中使用它,则需要按如下方式访问它,但是不能在实例化对象中使用它(新的UpgradeManager()创建实例)

UpgradeManager.tickValue

所以在你的例子中使用它。

public float GetMoneyPerSec()
{
    float tick = UpgradeManager.tickValue;
    // it cannot be used in a for-loop with each instance referencing it, static is a global, single value.
    return tick;
}

但你可能想做的就是这个

public float GetMoneyPerSec()
{
    float tick = UpgradeManager.tickValue / items.length;
    // it cannot be used in a for-loop with each instance referencing it, static is a global, single value.
    return tick;
}