Unity静态成员`UpgradeManager.tickValue'不能通过实例引用进行访问,而应使用类型名称对其进行限定 [英] Unity Static member `UpgradeManager.tickValue' cannot be accessed with an instance reference, qualify it with a type name instead

查看:184
本文介绍了Unity静态成员`UpgradeManager.tickValue'不能通过实例引用进行访问,而应使用类型名称对其进行限定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

How can i keep the structure like this, if the tickValue is static?

public float GetMoneyPerSec()
{
    float tick = 0;
    foreach (UpgradeManager item in items)
    {
        tick += item.tickValue;
    }
    return tick;
}

推荐答案

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

This error means your UpgradeManager looks as follows

public class UpgradeManager 
{
    public static float tickValue;
}

删除static关键字,它将在您遇到的问题中起作用.

remove the static keyword and it will work in the context you have in your question.

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

If you want to use it in a static context you need to access it as follows, but then you can not use it in an instanced object (new UpgradeManager() creates an instance)

UpgradeManager.tickValue

因此在您的示例中使用它.

so using it in your example.

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;
}

这篇关于Unity静态成员`UpgradeManager.tickValue'不能通过实例引用进行访问,而应使用类型名称对其进行限定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆