访问非静态成员''需要类型''的实例''

时间:2015-02-27 02:46:18

标签: unity3d unityscript

所以我理解这个问题,我想我的var是静态的。我明白了,但我做到了,仍然有同样的问题。显然我很容易忽视......

Inventory.js

private static var emptySlots:int;

public static function get EmptySlots():int{
    return emptySlots;
}
public static function set EmptySlots(value:int){
    emptySlots = value;
}

然后我在这里调用这些功能....

Slot.js

    if(IsEmpty()){
        ChangeSprite(slotEmpty, slotHighlight);
        Inventory.EmptySlots()++; // this is the line i try to reference 
    }

这是抛出的错误

Assets/Scripts/Slot.js(71,35): BCE0020: An instance of type 'Inventory' is required to access non static member 'EmptySlots'.

1 个答案:

答案 0 :(得分:0)

据我所知,

Unity Script不支持静态属性。虽然已经过时了,但请查看以下讨论:http://forum.unity3d.com/threads/static-getter-setter.59621/

无论如何,如果确实如此,其使用的正确语法将是:

Inventory.EmptySlots++;

所以,我想我会在UnityScript上坚持使用静态函数:

private static var emptySlots:int;

public static function getEmptySlots():int{
    return emptySlots;
}
public static function setEmptySlots(value:int){
    emptySlots = value;
}

if(IsEmpty()){
    ChangeSprite(slotEmpty, slotHighlight);
    Inventory.setEmptySlots(Inventory.getEmptySlots()+1);
}